アプリケーションのすべての HTTP リクエストを処理するクラスを実装したいと思います。これは基本的に次のようになります。
- ビジネスのリストを取得 (GET);
- ログイン (POST) を実行します。
- 場所を更新します (POST)。
そのため、サーバー (JSON) から結果文字列を取得し、それを別のメソッドに渡して応答を処理する必要があります。
私は現在、この方法を持っています:
public class Get extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... arg) {
String linha = "";
String retorno = "";
mDialog = ProgressDialog.show(mContext, "Aguarde", "Carregando...", true);
// Cria o cliente de conexão
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(mUrl);
try {
// Faz a solicitação HTTP
HttpResponse response = client.execute(get);
// Pega o status da solicitação
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Ok
// Pega o retorno
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// Lê o buffer e coloca na variável
while ((linha = rd.readLine()) != null) {
retorno += linha;
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return retorno;
}
@Override
protected void onPostExecute(String result) {
mDialog.dismiss();
}
}
public JSONObject getJSON(String url) throws InterruptedException, ExecutionException {
// Determina a URL
setUrl(url);
// Executa o GET
Get g = new Get();
// Retorna o jSON
return createJSONObj(g.get());
}
しかし、g.get()
空の応答が返されます。どうすれば修正できますか?