2013-03-15 181 views
1

我正在開發一個小型的android應用程序,我想通過put方法和圖像將圖像上傳到我的服務器,並將其作爲請求的主體與json object.I提供了很多關於這個和那個幫助的問題我很多。我試圖用下面的方式...從android應用程序將圖像上傳到服務器

class ImageUploadTask extends AsyncTask <Void, Void, Void>{ 
    @Override 
     protected Void doInBackground(Void... unused) { 

     HttpClient hc = new DefaultHttpClient(); 
      String message; 
      HttpPut p = new HttpPut("https://abc.com"); 
      JSONObject Jobject = new JSONObject(); 

      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bitmap.compress(CompressFormat.JPEG, 100, bos); 

      byte[] data = bos.toByteArray(); 

      Jobject.put("Image", data); 


     try { 
     message = Jobject.toString(); 

     p.setEntity(new StringEntity(message, "UTF8")); 
     p.setHeader("Content-type", "application/json"); 
      HttpResponse resp = hc.execute(p); 

      if (resp != null) { 
       if (resp.getStatusLine().getStatusCode() == 204) 
       { 

       } 
      } 

      Log.d("Status line", "" + resp.getStatusLine().getStatusCode()); 
     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
     return null; 


    } 

    @Override 
    protected void onProgressUpdate(Void... unsued) { 

    } 

    @Override 
    protected void onPostExecute(Void result) { 
     try { 
      if (dialog.isShowing()) 
       dialog.dismiss(); 

     } catch (Exception e) { 

      Toast.makeText(getApplicationContext(),"Error"+e, 
        Toast.LENGTH_LONG).show(); 
      Log.e(e.getClass().getName(), e.getMessage(), e); 
     } 
    } 
} 

但它不工作。當我試圖執行我的網絡調用時,它顯示我的系統錯誤 java.net.UnknownHostException: abc.com。我做錯了什麼。有沒有辦法做到這一點。需要幫忙。謝謝。

+0

你提供正確的鏈接 – 2013-03-15 06:04:01

+0

哪個環節?我的api鏈接正確嗎? – nilkash 2013-03-15 06:04:48

+3

您可能缺少INTERNET權限。 – Rajesh 2013-03-15 06:05:20

回答

0

我嘗試做同樣的我們也許可以幫助我們,這裏是我的代碼:

private class UploadFileToServer extends AsyncTask<Void, Void, Void> { 

    Bitmap image; 
    String name; 

    public UploadFileToServer(Bitmap img, String name){ 
     this.image = img; 
     this.name = name; 

    } 
    @Override 
    protected Void doInBackground(Void... params) { 
     //creation of bytearray 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     guardem al byteArrayOutputStream anterior 
     this.image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); 
     //codifing image 
     String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT); 
     //array list creation and add what we want to send 
     ArrayList<NameValuePair> dataToSend = new ArrayList<>(); 
     dataToSend.add(new BasicNameValuePair("image",encodedImage)); 
     dataToSend.add(new BasicNameValuePair("name",this.name)); 
     //i have dont understand so much from here to down.... 
     HttpParams HttpRequestParams = getHttpRequestParams(); 

     //send data by post server adres is my local api "ip/myapi/index.php" 
     HttpClient client = new DefaultHttpClient(HttpRequestParams); 
     HttpPost post = new HttpPost(SERVER_ADRESS+"index.php"); 

     try{ 
      post.setEntity(new UrlEncodedFormEntity(dataToSend)); 
//i take this part from you i hope it will work not tryied yet 
      HttpResponse response = client.execute(post); 
      if (response != null) { 
       if (response.getStatusLine().getStatusCode() == 204) 
       { 
         //so when you are here its all right? 
       } 
      } 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    private HttpParams getHttpRequestParams(){ 
     HttpParams HttpRequestParams = new BasicHttpParams(); 
     HttpConnectionParams.setConnectionTimeout(HttpRequestParams, 1000 * 30); 
     HttpConnectionParams.setSoTimeout(HttpRequestParams,1000*30); 
     return HttpRequestParams; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     Toast.makeText(getApplicationContext(), "image uploaded", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

我從youtuber得到的代碼,但它不起作用,你知道我是否必須發送迴應PHP或其自動完成的服務器? – Setar 2016-03-16 15:45:15

+0

o lol我沒讀過thad所以小解釋你解決了抱歉張貼這個我會嘗試你的完整代碼 – Setar 2016-03-16 15:51:07

相關問題