0
我想在android中編寫一個Http API。我正在使用AsyncTask來運行對我的Web服務的調用。我不想更新UI,而是隻需要在我的應用程序邏輯中使用的數據。這是我到目前爲止有:Android Http API
public class DataManager{
public static String result;
public DataManager()
{
}
public String get()
{
User user = new User();
user.execute("http://someuri/service/users/id/21001");
return user.getResult();
}
}
public class User extends AsyncTask <String,Void,String>{
private String result;
@Override
protected String doInBackground(String... arg0)
{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet (arg0[0]);
try
{
HttpResponse response = client.execute (get);
if (response.getStatusLine().getStatusCode() == 200)
{
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
}
}
catch (ClientProtocolException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
this.result = result;
}
public String getResult()
{
return result;
}
}
我想一個典型的電話是: String value = manager.get();
但是當我運行此我回來空。是什麼導致了這種情況,我如何重構此代碼以獲得所需的行爲。