数据正在服务器上更新/上传,但在Android中没有响应

时间:2015-12-17 10:01:22

标签: android json httpconnection

我正在尝试使用JsonParse类在服务器上发送数据。 现在当我尝试向服务器发出请求时,它总是给出SocketTimeOut Exception,但是在服务器端总是上传数据。 这意味着数据上传成功,但我没有得到客户端的响应。 我已从浏览器检查我的链接并获得完整的响应。

我的登录方案用户登录服务器,但我没有收到用户数据。

注意:

实际上当连接wifi或数据连接但速度慢/太慢时会发生此问题。

我已经搜索过这个问题,但找不到满意的解决方案。

这是JsonParse类

profileFields: [ 'email' , 'name' ]

,这是MainAcivity的请求

public class JSONParser {

    InputStream is = null;
    JSONObject jObj = null;
    String json = "";
    int timeoutConnection = 3000;

    JSONObject jsonObject = new JSONObject();
    JSONObject mainJson = new JSONObject();
    BufferedReader reader;
    Context context;

    // constructor
    public JSONParser(Context context) {
        this.context = context;
    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public String makeHttpRequest(String url, String method,
                                  List<NameValuePair> params) {


        //mobiledataenable(true);

        is = null;
        jObj = null;
        json = "";
        timeoutConnection = 3000;

        jsonObject = new JSONObject();
        mainJson = new JSONObject();

        // Making HTTP request
        try {
            // check for request method
            //if(method == "POST")
            if (method.equals("POST")) {
                // request method is POST
                // defaultHttpClient
                Log.e("JSONParser", "POST METHOD ");

                HttpParams httpParameters = new BasicHttpParams();
                // Set the timeout in milliseconds until a connection is established.
                // The default value is zero, that means the timeout is not used.
                int timeoutConnection = 10000;
                HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT)
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 15000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
                DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                //Log.e("1111.........",",,,,,,,,"+EntityUtils.toString(httpEntity) );
                is = httpEntity.getContent();
                Log.e("JSONParser is", is.toString());
                return bufferMethod();
            } else if (method == "GET") {

                HttpParams httpParameters = new BasicHttpParams();
                // Set the timeout in milliseconds until a connection is established.
                // The default value is zero, that means the timeout is not used.
                int timeoutConnection = 10000;
                HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT)
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 15000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
                String paramString = URLEncodedUtils.format(params, "utf-8");

                if (params.size() != 0)
                    url += "?" + paramString;

                Log.e("JSONParser", "GET METHOD..... " + url);

                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);

                // Log.e("2222*************.........", ",,,,,,,," + httpResponse.getEntity().getContent().toString());

                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

                Log.e("JSONParser is", is.toString());
                return bufferMethod();
            }
        } catch (SocketTimeoutException e) {
            //  Toast.makeText(getApplicationContext(), "Server timeout", Toast.LENGTH_LONG).show();
            Log.e("SOCK TIMEOUT", e.toString());
            jObj = null;
            json = new String();
            json = "5";
            return json;
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            jObj = null;
            json = new String();
            return json;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.e("UnsupportedEncodingException", e.toString());
            jObj = null;
            json = new String();
            return json;

        } catch (ClientProtocolException e) {
            e.printStackTrace();
            Log.e("ClientProtocolException", e.toString());
            jObj = null;
            json = new String();
            return json;

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("IOException", e.toString());
            jObj = null;
            json = new String();
            return json;
        }
        return bufferMethod();
    }

    public String bufferMethod() {

        try {
            reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            reader.close();
            json = new String();
            json = sb.toString();
            Log.e("json to string----", "...." + json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
            jObj = null;
            json = new String();
            if (e.toString().equalsIgnoreCase("java.net.SocketTimeoutException")) {
                json = "5";
                Log.e("222222----", "...." + json);
            } else {
                json = "6";
                Log.e("333333----", "...." + json);
            }
            return json;
        }

        return json;
    }


}

0 个答案:

没有答案