-1

my connection code is this to get string from website....

url value is http://sagindia.netne.net/sendmail.php

public String makeServiceCall(String url) {
    try {
      // http client
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpEntity httpEntity = null;
      HttpResponse httpResponse = null;

      HttpGet httpGet = new HttpGet(url);
      httpResponse = httpClient.execute(httpGet);
      httpEntity = httpResponse.getEntity();
      response = EntityUtils.toString(httpEntity);
   } catch (Exception e) {

   }
   return response;
}

showing error in line

httpResponse = httpClient.execute(httpGet);

log cat error part..

03-08 15:46:06.951: E/AndroidRuntime(1004): Caused by: anroid.os.NetworkOnMainThreadException

i have rtied to solve this problem , but couldn't solve this...i am first time trying to get response from server....if anybody have solution plz help me..

4

1 回答 1

0

我从@blackTigher 提供的链接中得到了答案...

问题是因为主线程上的网络...

所以在连接到 httpclient 之前添加了这段代码

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);

它对我有用...

有关更多详细信息,请访问NetworkOnMainThreadException

于 2014-03-08T11:44:47.740 回答