Android:当您向服务器发出POST请求时,请查看完整的URL

时间:2017-05-04 09:36:05

标签: android json url post

这是我的问题: 我有一个函数向服务器发出一个帖子请求,我在http://hmkcode.appspot.com/post-json/index.html上测试它似乎工作得很好。但是当我在我自己的服务器上尝试它应该收到特定的帖子请求(json)并给我一些json时,它不会发送给我任何东西。我的问题必须是我发送但我无法找到错误,所以你可以在POST时查看完整的URL(例如debbuger)?例如:" http://example.com/receive/data?{" id" = 12," name" ="示例"}" 。这是我的代码:

protected String doInBackground(String... strJson ) {
    URL url;
    String response = "";
    try {
        url = new URL(SERVER);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
        writer.write(strJson[0]);
        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));

            while ((line=br.readLine()) != null) {
                response+=line;
            }
        }
        else {
            response="NO HTTP_OK : " + responseCode;
        }
        conn.disconnect();
    } catch (MalformedURLException e) {
        response = "malformedURL" + e.toString();
    } catch (ProtocolException e) {
        response = "Protocol" + e.toString();
    } catch (IOException e) {
        response = "IOException" + e.toString();
    } catch (Exception e) {
        e.printStackTrace();
        response = "Exception" + e.toString();
    }

    return response;
}

1 个答案:

答案 0 :(得分:0)

试试这个

InputStream inputStream; 
HttpURLConnection urlConnection; 
byte[] outputBytes;

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

    @Override
    protected String doInBackground(Void... params) {

        try {
            URL url = new URL(Url);
            urlConnection = (HttpURLConnection) url.openConnection();
            outputBytes = query.getBytes("UTF-8");
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setConnectTimeout(15000);
            urlConnection.connect();

            OutputStream os = urlConnection.getOutputStream();
            os.write(outputBytes);
            os.flush();
            os.close();

            inputStream = new BufferedInputStream(urlConnection.getInputStream());
            ResponseData = convertStreamToString(inputStream);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
        return ResponseData;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


    }

    public String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append((line + "\n"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

以json字符串格式传递参数

JSONObject() params = new JSONObject();
params.put("key","your parameter");
params.put("key","your parameter");

query=params.toString();