Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:当您向服务器发出POST请求时,请查看完整url_Android_Json_Url_Post - Fatal编程技术网

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

Android:当您向服务器发出POST请求时,请查看完整url,android,json,url,post,Android,Json,Url,Post,我的问题是: 我有一个向服务器发出post请求的函数,我对它进行了测试,它似乎工作得很好。但当我在自己的服务器上尝试它时,它应该接收特定的post请求(json)并返回一些json,它不会向我发送任何内容。我的问题一定是我发送了什么,但我找不到错误,所以你能在发帖时检查完整的URL吗(以debbuger为例)?例如:“?{“id”=12,“name”=“example”}”。这是我的密码: protected String doInBackground(String... strJson ) {

我的问题是: 我有一个向服务器发出post请求的函数,我对它进行了测试,它似乎工作得很好。但当我在自己的服务器上尝试它时,它应该接收特定的post请求(json)并返回一些json,它不会向我发送任何内容。我的问题一定是我发送了什么,但我找不到错误,所以你能在发帖时检查完整的URL吗(以debbuger为例)?例如:“?{“id”=12,“name”=“example”}”。这是我的密码:

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;
}
试试这个

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();
    }
}

url.toString()
?那么
Log.d(“debugurl”,url.toString())呢?Log.e(“url”、“…”+url.tostring());然后检查您的日志。@Denny no当我执行url.toString()时,它会显示“”,但不是我发出的post请求。您可以使用截击发送post请求:
JSONObject() params = new JSONObject();
params.put("key","your parameter");
params.put("key","your parameter");

query=params.toString();