本文介绍了我怎样才能让Android上异步URL连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用下面的类连接到我的网络服务。我想使这个异步的。我怎样才能做到这一点?
包org.stocktwits.helper;
进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.InputStreamReader中;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.json.JSONException;
进口org.json.JSONObject;
进口android.util.Log;
公共类RestClient {
私有静态字符串convertStreamToString(InputStream的是){
/ *
*要InputStream中转换为字符串,我们使用使用BufferedReader.readLine()
* 方法。我们迭代直到BufferedReader中返回NULL,这意味着
*没有更多的数据读取。每个行会附加到一个StringBuilder
*和返回的字符串。
* /
的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(是));
StringBuilder的SB =新的StringBuilder();
串线= NULL;
尝试 {
而((行= reader.readLine())!= NULL){
sb.append(行+\ N);
}
}赶上(IOException异常E){
e.printStackTrace();
} 最后 {
尝试 {
is.close();
}赶上(IOException异常E){
e.printStackTrace();
}
}
返回sb.toString();
}
/ *这是一个测试功能,这将连接到一个给定的
*休息服务,并打印的响应到Android日志,
*标签Praeda。
* /
公共静态的JSONObject连接(字符串URL)
{
HttpClient的HttpClient的=新DefaultHttpClient();
// prepare请求对象
HTTPGET HTTPGET =新HTTPGET(URL);
//执行请求
HTT presponse响应;
尝试 {
响应= httpclient.execute(HTTPGET);
//检查响应状态
Log.i(Praeda,response.getStatusLine()的toString());
//弄个响应实体
HttpEntity实体= response.getEntity();
如果(实体!= NULL){
//一个简单的JSON响应读取
InputStream的河道= entity.getContent();
字符串结果= convertStreamToString(河道);
//一个简单的JSONObject创作
JSONObject的JSON =新的JSONObject的(结果);
//关闭输入流会触发连接释放
instream.close();
返回JSON;
}
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
返回null;
}
}
解决方案
在除了所有的Ladlestein的评论可能的解决方案,还有包装都在一个的AsyncTask 。
I am using the following class to connect to my web service. I would like to make this asynchronous. How can I do this?
package org.stocktwits.helper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class RestClient{
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
/* This is a test function which will connects to a given
* rest service and prints it's response to Android Log with
* labels "Praeda".
*/
public static JSONObject connect(String url)
{
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i("Praeda",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
// A Simple JSONObject Creation
JSONObject json=new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
return json;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
解决方案
In addition to all of the possible solutions in Ladlestein's comment, there's the simple answer of wrapping all that in an
AsyncTask
.
这篇关于我怎样才能让Android上异步URL连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!