2014-09-05 64 views
-4

我是新來的機器人!我需要你的幫助。我想創建一個android應用程序,用戶將提供一個圖像,該圖像將從android imageview,usnig asynctask和json發送到服務器,圖像的url將使用php存儲到服務器。然後,我想要顯示它回到android listview。我做了很多研究,但找不到它..請真的需要你的幫助。 。謝謝..上傳圖片到服務器和網址到數據庫,並返回到imageview

回答

0

您下面的代碼上傳圖像到服務器:

String url = //your URL for the server here; 

    MultipartEntity reqEntity = new MultipartEntity(
      HttpMultipartMode.BROWSER_COMPATIBLE); 

    File f = new File(Environment.getExternalStorageDirectory(), 
      "myImage.jpg"); 
    FileBody body = null; 
    if (f.exists()) { 
     body = new FileBody(f); 
     reqEntity.addPart(IMAGE, body); 
    } 
    reqEntity.addPart(IMAGEURL, new StringBody(f.getAbsolutePath()); 


    String urlString = getResponseStringFromURL(url, 30000, reqEntity); 

而對於getresponseStringFromUrl功能代碼爲:

/** 
    * Requests on Given URL for Response with a Given Timeout 
    * 
    * @param reqEntity 
    */ 
    public static String getResponseStringFromURL(String url, int timeOut, 
      MultipartEntity reqEntity) { 
     StringBuilder result = new StringBuilder(); 
     HttpParams httpParameters = new BasicHttpParams(); 
     int timeoutConnection = timeOut; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 
       timeoutConnection); 
     int timeoutSocket = timeOut; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
     DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); 
     HttpPost request = new HttpPost(url); 
     if (reqEntity != null) { 
      request.setEntity(reqEntity); 
     } 
     HttpResponse response = null; 

     try { 
      response = httpClient.execute(request); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     HttpEntity entity = response.getEntity(); 
     InputStream input = null; 
     try { 
      input = new BufferedInputStream(response.getEntity().getContent()); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     byte data[] = new byte[40000]; 

     long totalContactsCount = -1; 
     int readContactsCount = 0; 
     int currentByteReadCount = 0; 

     /** read response from inpus stream */ 
     try { 
      while ((currentByteReadCount = input.read(data)) != -1) { 
       String readData = new String(data, 0, currentByteReadCount); 
       result.append(readData); 

       // then +1 progress on every ...},{... (JSON object separator) 
       if (readData.indexOf("}~{") >= 0) { 
        readContactsCount++; 
       } 

       /* 
       * // publishing the progress.... if (totalContactsCount > 0) { 
       * publishProgress((int)(readContactsCount * 100/
       * totalContactsCount)); } 
       */ 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      input.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     /** transform response into JSONArray */ 
     return result.toString(); 

    }