没有获取URL数据,为什么这个“网址错误”

时间:2013-04-23 07:21:25

标签: android

我从网上获取数据时遇到问题。找不到ul,怎么发生这个。 Logcat的主要错误:

04-23 01:00:04.739:W / HttpPostRetreiver(11853):网址错误> 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

如何得到此错误,我无法找到,任何人请带着问题尝试解决。

先谢谢。快乐编码!!!

2 个答案:

答案 0 :(得分:1)

  

android.os.NetworkOnMainThreadException

你正在尝试在UI线程上执行网络操作,android 3+设备不允许在UI线程上进行网络操作。因此它会抛出NetworkOnMainThreadException,因此请使用AsyncTask或单独thread来执行网络操作

答案 1 :(得分:1)

android.os.NetworkOnMainThreadException

表示您正在对UI thread执行屏蔽操作。这是Android 3.0禁止的,你应该使用Thread。阅读此guide