Friday, April 18, 2014

java.net.InetAddress.getByName program exit? ?


posts by CCDDzclxy edited 2013-09-17 20:50:53
RT
In " http://www.devdiv.com/Android-_ip_ -thread-52325-1-1.html "in Android found inside the DNS resolution is getByName functions.

However, when I use getByName or getAllByName, procedures are being given directly out of the ...
Please help us to see how the matter? ?

do or say Android inside DNS resolution, not with getByName? That is what is the use? ?

Click the button btnDns when the error directly out of the ...
Error in Figure:


The following code is run on a virtual machine (xp + eclipse):
 

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import java.net *;.

public class MainActivity extends Activity
{
public EditText etMsg;

@ Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

/ / ***

etMsg = (EditText) findViewById (R.id.etMsg);
Button btnDns = (Button) findViewById (R.id.btnDns);
btnDns.setOnClickListener (new Button.OnClickListener ()
{
@ Override
public void onClick (View v)
{
try
{
InetAddress x = InetAddress.getByName ("www.baidu.com");
}
catch (Exception e)
{
etMsg.append (e.getMessage ());
}
}
});

}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}

}


Please help, thank you.


<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
You should not look at logcat exception information inside the country. Should be the question of powers. You see is not.
Reply:
cited a floor Jia_H reply:
you do not see logcat exception information inside the country. Should be the question of powers. You see is not.


Novice, do not understand ... that there seems to be a null pointer exception? ?
Reply:
cited a floor Jia_H reply:
you do not see logcat exception information inside the country. Should be the question of powers. You see is not.

Permission to do so, whether it is to say this:
I have added the following two sentences in AndroidManifest.xml inside:


Reply:
Yeah, debug it. Which shows that one out in append a null reference exception.

Description:
An error occurred while a call getName, should be the question of powers just said, you need to add access to the network in the xml file permissions
2 Check that etmsg id and xml inside are the same
Reply:
references, 4th Floor Jia_H reply:
Yeah, debug it. Which shows that one out in append a null reference exception.

Description:
An error occurred while a call getName, should be the question of powers just said, you need to add access to the network in the xml file permissions
2 Check that etmsg id and xml inside are the same

Indeed, I etMsg.append (e.getMessage ()); into etMsg.append ("err"); had "err" is displayed, it shows getByName wrong ah, yes permissions problem? ? I added two lines of code on the 3rd floor, ah ...
Reply:
I changed:
 
catch (Exception e)
{
String strErr = e.getMessage ();
etMsg.append ("err:" + strErr);
}

Will print out: "err: null". This shows that description? ? ? ...
Reply:
I added a:
 
android: id = "@ + id/textView1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_below = "@ + id / btnDns"
android: layout_marginTop = "14dp"
android: autoLink = "all"
android: text = "http://www.baidu.com/" />

Click the link, the page can be opened baidu should have access permissions explain it ...
Reply:
Is e.getMessage () returns null, and a bit strange. With e.printTraceStack output to see if there can be referenced content.
Reply:
reference to the 8th floor Jia_H reply:
is e.getMessage () returns null, and a bit strange. With e.printTraceStack output to see if there can be referenced content.

Into this:
 
catch (Exception e)
{
/ / EtMsg.append (e.getMessage ()); / / directly so wrong ...
String strErr = e.getMessage ();
etMsg.append ("err:" + strErr + "\ r \ n");

StringWriter sw = new StringWriter ();
PrintWriter pw = new PrintWriter (sw);
e.printStackTrace (pw);
/ / System.out.println (sw.toString () toUpperCase ().);
etMsg.append (sw.toString () toUpperCase ().);
}

Output information:
(Not to fetch the files inside the virtual machine ... only print out, cut a four figure ... I saw did not understand what the problem ...)




Reply:
Found, is NetworkOnMainThreadException, the main thread can not restrict access to the network:
http://www.eoeandroid.com/thread-272038-1-1.html
Reply:
Yes, forget this crop up.

No comments:

Post a Comment