将长编码字符串上传到android中的服务器时java.net.SocketTimeoutException

时间:2016-07-27 04:21:41

标签: java android http

我正在使用httpok库来处理网络请求。如果我只发送ID,姓名,年龄等,代码工作正常  但是当我发送“encodedPhotoString”时 要上传到服务器的图像的长字符串,大约8000个字符,代码抛出java.net.SocketTimeoutException:timeout 例外。我试图改变连接超时和读取超时,但这对我不起作用。我正在使用namecheap.com托管服务器。 请帮我解决这个问题。谢谢。

 public class AddUserAsync extends AsyncTask<Void,Void,Void> {
    OkHttpClient client=null;
    RequestBody formBody=null;
    Request request=null;
    JSONObject json = null;
    String fromServer = "";
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(UploadImage.this);
        pDialog.setMessage("Creating Account...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected Void doInBackground(Void... voids) {
        GetUserDetails();
        GenerateGCMID();
        email= email.trim().toLowerCase();
        latitude = GPSTracker.getLatitude();
        longitude =  GPSTracker.getLongitude(); 
        client = new OkHttpClient().newBuilder().readTimeout(30,TimeUnit.SECONDS).connectTimeout(30,TimeUnit.SECONDS).build();
        formBody = new FormBody.Builder()
                .add("ID",ID)
                .add("Name",name)
                .add("email",email)
                .add("password",password)
                .add("age",age)
                .add("number",number)
                .add("bloodgroup",bloodgroup)
                .add("lat",latitude+"")
                .add("longi",longitude+"")
                .add("image",encodedPhotoString)
                .build();
        request = new Request.Builder()
                .url("http://faceblood.website/blood_app/Adduser.php")
                .post(formBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String res = response.body().string();
            json = new JSONObject(res);
            fromServer = json.getString("added");
            Log.e("stringtest",json.getString("added"));
        } catch (IOException e) {
            Log.e("IOException",e.toString());
            e.printStackTrace();
        } catch (JSONException e) {
            Log.e("JSONException",e.toString());
            e.printStackTrace();
        }      
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        pDialog.dismiss();

        Log.e("fromServer",fromServer);
        if(fromServer.equals("addeduser")){
            Toast.makeText(getBaseContext(), "Created Successfully", Toast.LENGTH_LONG).show();
            onSignupSuccess();
        }else {
            Toast.makeText(getBaseContext(), "Network problem. Try again", Toast.LENGTH_LONG).show();
        }
    }
}

<?php
require "conn.php";

$id = $_POST["ID"];

$name = $_POST["Name"];

$email = $_POST["email"];

$password = $_POST["password"];

$age = $_POST["age"];

$number = $_POST["number"];

$blood = $_POST["bloodgroup"];

$latitude = $_POST["lat"];

$longitude = $_POST["longi"];

$image = $_POST["image"];

$splitEmail = explode('@', $email);
$imageValue = base64_decode($image);
$image = $splitEmail[0].".jpg";
$path = "images/".$image;
file_put_contents($path, $imageValue);


$sql = "INSERT INTO UserDetails (ID,Name,email,password,age,number,bloodgroup,lat,longi,image)
VALUES ('$id','$name','$email','$password','$age','$number','$blood','$latitude','$longitude','$image')"; 
	


if ($conn->query($sql) === TRUE) {
    $response["added"] = "addeduser";
} else {

}

echo json_encode($response); 
$conn->close();
?>

1 个答案:

答案 0 :(得分:0)

你忘了设置writeTimeout

client = new OkHttpClient().newBuilder().readTimeout(30,TimeUnit.SECONDS).connectTimeout(30,TimeUnit.SECONDS).writeTimeout(1, TimeUnit.MINUTES).build()