如何在android中使用使用json的post方法

时间:2018-10-25 10:06:30

标签: javascript java android

如何在Android enter code here公共类HttpHandler {     私有静态最终字符串TAG = HttpHandler.class.getSimpleName();

public HttpHandler() {
}


public String makeServiceCall(String reqUrl) {
    String response=null;

    try
    {
        URL url=new URL(reqUrl);
        HttpURLConnection connection=(HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");

        InputStream inputStream=new BufferedInputStream(connection.getInputStream());
        response=convertStreamToString(inputStream);

    } catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }

    return response ;
}
private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}`enter code here`

0 个答案:

没有答案