从播放中检索 Json 响应 Android

Retrieve a Json Response Android from Play

我在 "heroku" 上托管了一个应用程序。它具有您通过 get 调用它的方法,它将以 "Json code"

响应
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)

现在我的 android 代码

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) {}

我想接收我的游戏应用程序发送的 JSON 代码,并在我的 android 应用程序中使用它。

试试这个代码,

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" />