应用程序经常在httprequest上崩溃

时间:2015-05-12 06:31:58

标签: android mysql json apache httpclient

从服务器获取数据时,我经常收到错误消息。从过去几周开始,它正在发生。有时它工作,有时不工作。 logcat显示错误在jsonparser文件中

def upload(bucketName: String,  keyName: String,  file: File,  contentLength: Long,  contentType: String,  serverSideEncryption: Boolean = true,  storageClass: StorageClass = StorageClass.ReducedRedundancy ):Upload = {
  val metaData = new ObjectMetadata
  metaData.setContentType(contentType)
  metaData.setContentLength(contentLength)

  if(serverSideEncryption) {
    metaData.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION)
  }

  val putRequest = new PutObjectRequest(bucketName, keyName, file)
  putRequest.setStorageClass(storageClass)
  putRequest.getRequestClientOptions.setReadLimit(100000)
  putRequest.setMetadata(metaData)
  tx.upload(putRequest)

}
}

但无法纠正原因?

  

exercise.java

httpclient.execute
  

jsonparser.java

List<NameValuePair> parametres = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            parametres.add(new BasicNameValuePair("inputChar", inputChar));



            **JSONObject json = jsonParser.makeHttpRequest(url_display_user,
                    "GET", parametres);**


            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    exercises = json.getJSONArray(TAG_RESPONSE);
                    // looping through All RESPONSE
                    for (int i = 0; i < exercises.length(); i++) {
                        JSONObject jsonobj = exercises.getJSONObject(i);
                        // Storing each json item in variable
                        String id = jsonobj.getString(TAG_ID);
                        String activityname = jsonobj.getString(TAG_ACTIVITY_NAME);
                        String metvalue = jsonobj.getString(TAG_METVALUE);
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                        map.put(TAG_ID, id);
                        map.put(TAG_ACTIVITY_NAME, activityname);
                        map.put(TAG_METVALUE, metvalue);
                        // adding HashList to ArrayList
                        exerciseitems.add(map);

                    }
                    return json.getString(TAG_MESSAGE);

                } else {
                    return json.getString(TAG_MESSAGE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
  

我在获取数据时遇到此错误   error

再错一次 enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用启用了SSL的httpurlconnection,并且比httpclient更安全。

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

    private String URL;
    private JSONObject jsonObjSend;
    int mResponseCode = -1;
    private Object totaltime;
    private long starttime;
    public HttpPostData(String URL, JSONObject jsonObjSend) {
        this.URL = URL;
        this.jsonObjSend = jsonObjSend;
    }

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


        String finalResult = null;
        URL url = null;

            HttpsURLConnection urlConnection = null;
            try {
                url=new URL(URL);
                urlConnection = (HttpsURLConnection) url.openConnection();
                urlConnection.setDoOutput(true);  
                urlConnection.setDoInput(true);   

                urlConnection.setRequestMethod("POST");  
                urlConnection.setUseCaches(false);  
                urlConnection.setConnectTimeout(500);  
                urlConnection.setReadTimeout(500);  
                urlConnection.setRequestProperty("Content-Type","application/json"); 
                urlConnection.connect();  


                OutputStreamWriter out = new   OutputStreamWriter(urlConnection.getOutputStream());
                out.write(jsonObjSend.toString());
                out.close(); 
                starttime = System.currentTimeMillis();

                try {
                    mResponseCode = urlConnection.getResponseCode();
                    totaltime = System.currentTimeMillis() - starttime;
                    Log.e("HTTP POST Response Code:", ""
                            + mResponseCode);
                } catch (SSLPeerUnverifiedException e) {

                Log.e("Exception", Log.getStackTraceString(e));
            } catch (IOException e) {

                Log.e("Exception", Log.getStackTraceString(e));
                Log.e("HTTP POST Response Code(2):", ""
                        + mResponseCode);
            }