0

我从网络获取数据有问题。找不到 ul,如何发生这种情况。Logcat 的主要错误:

04-23 01:00:04.739: W/HttpPostRetreiver(11853): URL > http://test.com/url_getting_Info.php错误

我有 Java 代码:

String countryId = sharedPreferences.getString("CountryId ", "");
    String capitalId = sharedPreferences.getString("CapitalId ", "");

  String input = String.format("<Es_Request><Data><CountryId>%s</CountryId><CapitalId >%s</CapitalId ></Data></Es_Request>",countryId , capitalId );

String response = HttpPostRetreiver.retriver(Url.url_getting_Info, input,DisplayData.this);

if (response != null) {
//DO something
}
else
{
Log.d("hello", "i get this message");
}

这里是HttpPostReceiver.java

public class HttpPostRetreiver {

public static String retriver(String Url, String input, Context context) {
    Log.d("TESTING", "URL->>" + Url);
    String responseString = null;
    StringEntity stringEntity;
    HttpPost postRequest = new HttpPost(Url);
    try {

        Log.e("string is", input + "\n" + Url);
        stringEntity = new StringEntity(input, "UTF-8");
        stringEntity.setContentType("application/atom+xml");

        postRequest.setEntity(stringEntity);
        Log.v("Post", "Posted");

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(postRequest);
        HttpEntity getResponseEntity = response.getEntity();

        responseString = EntityUtils.toString(getResponseEntity);

    } catch (Exception e) {
        postRequest.abort();
        Log.w("HttpPostRetreiver", "Error for URL " + Url, e);
    }

    return responseString;

}

**04-23 01:00:04.729: E/string is(11853): http://test.com/url_getting_Info.php
04-23 01:00:04.729: V/Post(11853): Posted
04-23 01:00:04.739: W/HttpPostRetreiver(11853): Error for URL >http://test.com/url_getting_Info.php
04-23 01:00:04.739: W/HttpPostRetreiver(11853): android.os.NetworkOnMainThreadException
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:674)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at sdei.edustatusnew.services.HttpPostRetreiver.retriver(HttpPostRetreiver.java:31)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at sdei.edustatusnew.ChildActivity.getChildList(ChildActivity.java:238)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at sdei.edustatusnew.ChildActivity.onCreate(ChildActivity.java:206)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.app.Activity.performCreate(Activity.java:4470)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.os.Looper.loop(Looper.java:137)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at android.app.ActivityThread.main(ActivityThread.java:4517)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at java.lang.reflect.Method.invoke(Method.java:511)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
04-23 01:00:04.739: W/HttpPostRetreiver(11853):     at dalvik.system.NativeStart.main(Native Method)
04-23 01:00:04.749: D/hello(11853): i get this message

如何得到这个错误,我找不到,任何人请带着问题并尝试解决。

在此先感谢。快乐编码!

4

2 回答 2

1

android.os.NetworkOnMainThreadException

您正在尝试在 UI 线程上执行网络操作,android 3+ 设备不允许在 UI 线程上进行网络操作。所以会抛出NetworkOnMainThreadException,所以使用AsyncTaskor 分开thread来执行网络操作

于 2013-04-23T07:24:01.643 回答
1
android.os.NetworkOnMainThreadException

表示您正在对UI thread. 这是禁止的Android 3.0,因为您应该使用 Thread。阅读本指南

于 2013-04-23T07:24:07.567 回答