2017-01-12 2 views
-5

Ich habe ein Programm in Android für das Hochladen von Dateien erstellt. Alles funktioniert perfekt. Aber, wenn ich eine Datei laden, die mehr als 1 MB hat, habe ich diesen Fehler von meinem Server: „Fehler beim Hochladen“Ich bin nicht in der Lage, mehr als 1 mb-Datei in Android hochladen

Hier meine Upload ist() -Methode

private void uploadFile() { 
     dialog = ProgressDialog.show(getActivity(), "", "Uploading File...", true); 


     // Map is used to multipart the file using okhttp3.RequestBody 
     Map<String, RequestBody> map = new HashMap<>(); 
     long maxLength = 10000000; 
     File file = new File(selectedFilePath); 
     if(file.length() > maxLength){ 
      Toast.makeText(getActivity(), "can't upload file if size more than 10mb", Toast.LENGTH_LONG).show(); 
      dialog.dismiss(); 

     }else { 
      String name = tv_name.getText().toString(); 
      String email = tv_email.getText().toString(); 
      // Parsing any Media type file 
      RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file); 
      RequestBody requestBody1 = RequestBody.create(MediaType.parse("text/plain"), name); 
      RequestBody requestBody2 = RequestBody.create(MediaType.parse("text/plain"), email); 
      map.put("file\"; filename=\"" + selectedFilePath + "\"", requestBody); 
      map.put("name\"; username=\"" + name + "\"", requestBody1); 
      map.put("email\"; email=\"" + email + "\"", requestBody2); 

      ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class); 
      Call<ServerResponse> call = getResponse.upload("token", map); 
      call.enqueue(new Callback<ServerResponse>() { 
       @Override 
       public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) { 
        if(dialog.isShowing()){ 
         dialog.dismiss(); 
        } 
        ServerResponse serverResponse = response.body(); 
        if (serverResponse != null) { 
         if (serverResponse.getSuccess()) { 
          Toast.makeText(getActivity(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show(); 

         } else { 
          Toast.makeText(getActivity(), serverResponse.getMessage(), Toast.LENGTH_SHORT).show(); 
         } 
        } else { 
//     Log.v("Response", serverResponse.toString()); 
        } 
        // dialog.dismiss(); 
        goToProfile(); 


       } 

       @Override 
       public void onFailure(Call<ServerResponse> call, Throwable t) { 
        t.printStackTrace(); 
        dialog.dismiss(); 
        Toast.makeText(getActivity(), "Failed, Please Try Again", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    } 

TimeOut

OkHttpClient client = new OkHttpClient.Builder() 
     .connectTimeout(5, TimeUnit.MINUTES) 
     .writeTimeout(5, TimeUnit.MINUTES) 
     .readTimeout(5, TimeUnit.MINUTES).build(); 

return new Retrofit.Builder() 
     .baseUrl(AppConfig.BASE_URL).client(client) 
     .addConverterFactory(GsonConverterFactory.create()) 
     .build(); 

}

Es ist mein PHP-Code

$target_dir = "uploads/"; 
$target_file_name = $target_dir .basename($_FILES["file"]["name"]); 
$response = array(); 

// Check if image file is a actual image or fake image 
if (isset($_FILES["file"])) { 
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file_name)) { 
     $success = true; 
     $message = "Successfully Uploaded"; 
    } else { 
     $success = false; 
     $message = "Error while uploading"; 
    } 
} else { 
     $success = false; 
     $message = "Required Field Missing"; 
} 

$response["success"] = $success; 
$response["message"] = $message; 
echo json_encode($response); 

?> 
+0

Verengen Sie es. Probieren Sie den PHP-Code von Ihrem Browser oder CURL. –

+3

der gleiche Code hier ... fast die gleiche Frage: http://StackOverflow.com/Questions/41610385/my-progressdialog-is-not-disappearing ... Bitte aufhören zu Dummy-Konten erstellen und die gleiche Frage – Selvin

+0

http://shaoniiuc.com/android/image-upload-retrofit-library/ – william

Antwort

0

Versuchen Sie, die Datei zu komprimieren und erneut hochzuladen.

+0

ich Komprimierte haben, aber es ist nicht hochgeladen .. bitte helft mir .. – william

0

Mit dieser Konfiguration von Retrofit:

public static OkHttpClient okHttpClient1 = new OkHttpClient().newBuilder() 
      .connectTimeout(150, TimeUnit.SECONDS) 
      .writeTimeout(150, TimeUnit.SECONDS) 
      .readTimeout(150, TimeUnit.SECONDS) 
      .build(); 
    private static Retrofit retrofit = null; 

    public static Retrofit getClient() { 
     if (retrofit == null) { 
      retrofit = new Retrofit.Builder() 
        .baseUrl(AppConfig.BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .client(okHttpClient1) 
        .build(); 
     } 
     return retrofit; 
    } 

kurz gesagt, ich will sagen, Erhöhung Ihre Timeout auf Android Seite.

+1

ich habe es public static Retrofit getRetrofit() { OkHttpClient client = new OkHttpClient.Builder verwendet() .connectTimeout (5, TimeUnit.MINUTES) .writeTimeout (5, TimeUnit.MINUTES) .readTimeout (5, TimeUnit.MINUTES) .build(); return new Retrofit.Builder() .baseUrl (AppConfig.BASE_URL) .client (Client) .addConverterFactory (GsonConverterFactory.create()) .build(); } – william

Verwandte Themen