【问题标题】:Retrieve a Json Response Android from Play从 Play 中检索 Json 响应 Android
【发布时间】:2015-05-22 12:02:01
【问题描述】:

我有一个托管在“heroku”上的应用程序。它有这个方法,您可以通过 get 调用它,它会以“Json 代码”响应

public static Result renderManga(int id) {
    Manga m = Manga.find.byId(id);

    if (m != null) {
        return ok(Json.toJson(m));
    } else
        return null;
    }
}

路由文件

GET /srvc/manga/:id                    controllers.Services.renderManga(id:Integer)

现在是我的安卓代码

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response = httpclient.execute(new HttpGet("https://boku-no-manga.herokuapp.com/srvc/manga/5"));
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        String responseString = out.toString();
        EditText res = (EditText) findViewById(R.id.result);
        res.setText(responseString);
        Log.i("JSONmymanga",responseString);
        out.close();

    } else {

        response.getEntity().getContent().close();
        throw new IOException(statusLine.getReasonPhrase());
    }

} catch (Exception e) {}

我想接收我的 play 应用发送的 JSON 代码并在我的 android 应用中使用它。

【问题讨论】:

  • 那么你还在纠结 JSON 解析吗?
  • 我还没到那里,我什么都没收到......

标签: java android json playframework-2.2


【解决方案1】:

试试这个代码,

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;


public class ServerTest extends Activity {

    private String TAG = "test";
    private String url = "https://boku-no-manga.herokuapp.com/srvc/manga/5";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new Download().execute();
    }


    public class Download extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            String out = null;

            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();

                final HttpParams httpParameters = httpClient.getParams();

                HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
                HttpConnectionParams.setSoTimeout(httpParameters, 15000);

                HttpGet httpPost = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                out = EntityUtils.toString(httpEntity, HTTP.UTF_8);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return out;
        }


        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.e(TAG, result);
        }
    }
}

还要确保您已将此添加到清单中,

<uses-permission android:name="android.permission.INTERNET" />

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    相关资源
    最近更新 更多