2013-04-23 23 views
0

我是從Web獲取數據的問題。找不到UL,如何發生這種情況。在logcat的 主要錯誤:未獲取URL數據,爲什麼此「網址錯誤」

1月4日至23日: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 

如何得到這個錯誤,我無法找到答案,請人用問題的考慮d試着解決。

在此先感謝。快樂編碼!

+0

你可以打印異常堆棧跟蹤並在此處添加它.. – 2013-04-23 07:27:46

回答

1

android.os.NetworkOnMainThreadException

你試圖在UI線程中執行網絡操作,機器人3+設備不允許在UI線程網絡操作。因此,它會拋出NetworkOnMainThreadException,所以使用AsyncTask或獨立thread執行網絡操作

1
android.os.NetworkOnMainThreadException 

意味着你正在執行的UI thread阻塞opertation。這是禁止的,因爲Android 3.0,你應該使用Thread。閱讀本文guide

相關問題