我想在后台线程中发送一个get请求,并拥有以下代码:
public HttpResponse sendRequestInBackground(String clickUrl){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(clickUrl);
HttpContext context = new BasicHttpContext();
response = client.execute(request, context);
Log.d("SEND_GET_REQUEST", "GET REQUEST SENT WITH URL: " + clickUrl);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return response;
}
但是继续java.lang.IllegalStateException: Scheme 'market' not registered.
任何想法,我做错了什么?
答案 0 :(得分:0)
您不是以这种方式创建后台线程,只是通过强制允许主线程上的网络操作来阻止您的应用程序崩溃。执行此类操作的有效方法是使用AsyncTask类。顺便说一句下面的代码,每次都能为我提供http获取请求。
HttpGet httpRequest = new HttpGet(url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = null;
try {
response = (HttpResponse) httpclient.execute(httpRequest);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 1 :(得分:0)
这些链接可能会对您有所帮助:
Get the final URL of a redirected request with unconventional scheme
https://groups.google.com/forum/#!topic/android-developers/jh9HAbIUn48
Android, how can i encapsulate an AsyncTask class with network connections
Query in AsyncTask giving illegalStateException
Get the final URL of a redirected request with unconventional scheme
答案 2 :(得分:0)
看起来您发送的网址格式不正确。我猜你的网址是这样的:market://details?id=com.example.bacon
这不是有效的网址。如果您尝试将Play商店打开到想要使用Intent的应用程序。
像这样(其中com.example.bacon是你的应用程序的包名):
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.example.bacon"));
startActivity(intent);
没有更多的代码发布,这是我能做的最好的。
答案 3 :(得分:0)
我使用http://loopj.com/android-async-http/,它对我来说很有效。