json Post通过HttpURLConnection

时间:2014-03-28 06:03:26

标签: android json

我正在尝试发送json。但是我有一个错误如下:com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:203)

@覆盖

        protected String doInBackground(String... arg0) {
            try {
                URL url = new URL(AppConstant.SEND_ANSWER + _survey_id + "/answers/");
                System.out.println("SEND URL " + url);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");  
                connection.setDoInput(true);
                connection.setDoOutput (true);
                connection.setUseCaches (false);
                connection.setRequestProperty("Auth-Token", _token = mSettings.getString("token", ""));
                connection.setRequestProperty("Content-Type","application/json");
                connection.connect();
                int status = connection.getResponseCode();

                JSONObject params = new JSONObject();
                JSONArray jsonArray = new JSONArray();
                JSONObject studentsObj = new JSONObject();

                int listSize = _q_list.size();
                for (int i = 0; i<listSize; i++) {
                      question = question_list.get(i).id;
                      params.put("question", question);
                      params.put("answer", "true");
                      jsonArray.put(params);
                      studentsObj.put("id", _page_id);
                      studentsObj.put("answers", jsonArray);

                      String jsonStr = studentsObj.toString();  
                      System.out.println("jsonString: "+ jsonStr);
                      OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
                      wr.write(studentsObj.toString());
                      wr.flush();
                      wr.close();
                }       
                 BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
                    String line = null;
                    String response = "";
                    while ((line = rd.readLine()) != null) {
                       response += line;
                    }
                System.out.println("Params " + params); 
                System.out.println("Connection " + status);
            } catch (Exception e) {
                e.printStackTrace();
            }               
            return null;

        }

但我有一个错误:com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:203)

1 个答案:

答案 0 :(得分:0)

尝试将此传递url和json对象作为字符串。

    HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 20000);
            HttpConnectionParams.setSoTimeout(myParams, 20000);

            String json_o = paramenter;
            HttpPost httppost = new HttpPost(URL);

            // set header
            httppost.setHeader("Content-type", "application/json");

            try {
                StringEntity se = new StringEntity(json_o);
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));

                httppost.setEntity(se);

                HttpResponse response = httpclient.execute(httppost);

                temp = EntityUtils.toString(response.getEntity());
}catch (IOException e) {
            e.printStackTrace();
        }