使用onProgressUpdate发布进度百分比

时间:2019-05-31 15:45:12

标签: json android-asynctask upload httpurlconnection postdata

我使用异步任务发送字符串。我使用HttpURLConnection这样做。 我想使用onProgressUpdate方法并为用户提供一定的进度。

我发现了很多有关上载文件的示例,但没有上载JSON对象的示例。

如何使用下面的代码

protected String doInBackground(String... arg0) {

            try{

                URL url = new URL("https://abc");

                JSONObject postDataParams = new JSONObject();

                String id= "123";
                postDataParams.put(string1,myarray[0]);
                postDataParams.put(string2,myarray[1]);
                postDataParams.put(string3,myarray[2]);
                postDataParams.put(string4,myarray[3]);
                postDataParams.put(string5,myarray[4]);
                postDataParams.put("id",id);

                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(15000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("POST");
                conn.setDoInput(true);
                conn.setDoOutput(true);
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                writer.write(getPostDataString(postDataParams));
                writer.flush();
                writer.close();
                os.close();
                int responseCode=conn.getResponseCode();
                if (responseCode == HttpsURLConnection.HTTP_OK) {
                    BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuffer sb = new StringBuffer("");
                    String line="";
                    while((line = in.readLine()) != null) {
                        sb.append(line);
                        break;
                    }
                    in.close();
                    return sb.toString();

                }
                else {
                    return new String("false : "+responseCode);
                }
            }
            catch(Exception e){
                return new String("Exception: " + e.getMessage());
            }


        }

        protected void onProgressUpdate(Integer... values) {
            Log.e("Value", "onProgressUpdate - " + (values[0] * 100 / maxProgress) + "%");
        }

这是getPostDataString()

public String getPostDataString(JSONObject params) throws Exception {

        StringBuilder result = new StringBuilder();
        boolean first = true;

        Iterator<String> itr = params.keys();

        while(itr.hasNext()){

            String key= itr.next();
            Object value = params.get(key);

            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(key, "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(value.toString(), "UTF-8"));

        }
        return result.toString();
    }

0 个答案:

没有答案