2012-05-21 74 views
0

我一直在尋找兩個星期,如何使用Android上的發佈發佈數據,我到處尋找,嘗試了每個解決方案,但它仍然無法正常工作。 現在,我只需要發佈一個字符串鏈,然後我將不得不發佈帶有一些字符串鏈的圖片。我的實際代碼是:在Android上使用POST發佈數據

public String Publication() 
{ 
    try 
    { 
     Intent intentget = getIntent(); 
     String titre = intentget.getExtras().getString("titre"); 
     String poster = titre +"="+getTableau(_tlab, iNbLignes, iNbCol); 
     Log.d("reponse","gettableau"); 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost("http://tweet.envrac.ch/Publication.php"); 
     Log.d("reponse","url"); 
     List<NameValuePair> pairs = new ArrayList<NameValuePair>(1); 
     pairs.add(new BasicNameValuePair("LabColler", poster)); 
     Log.d("reponse","pairs"); 
     post.setEntity(new UrlEncodedFormEntity(pairs)); 
     Log.d("reponse","encode"); 
     //bug ici 
     HttpResponse response = client.execute(post, new BasicHttpContext()); 
     Log.d("reponse","post"); 
     return "reponse = "+response; 
    } 
    catch (Exception e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return "erreur"; 
    } 

} 

希望你能幫助我。

+0

具有u添加所需的權限在清單? – blessenm

+0

是的,當然,我做了^^ – Jen

+0

它看起來像不執行,行「HttpResponse response = client.execute(post,new BasicHttpContext());」。 – Jen

回答

0

也許這段代碼可以幫助你。

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

     nameValuePairs.add(new BasicNameValuePair("firstname", str_firstName)); 
     nameValuePairs.add(new BasicNameValuePair("middlename", str_middleName)); 
     nameValuePairs.add(new BasicNameValuePair("lastname", str_lastName)); 

     try{ 
      HttpClient httpClient = new DefaultHttpClient(); 
      URI uri = URIUtils.createURI("http","www.abc.com", -1,        "dosomething.php", 
        URLEncodedUtils.format(nameValuePairs, "UTF-8"), null); 
      Log.i("URI : " , uri.toString()); 
      HttpPost httpPost = new HttpPost(uri); 
      HttpResponse response = httpClient.execute(httpPost); 

      HttpEntity entity = response.getEntity(); 
      }catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show(); 
     } 
+0

謝謝,但它不起作用 – Jen

0

我改變你的代碼一點,試試這個

public String Publication() 
{ 
    try 
    { 
     Intent intentget = getIntent(); 
     String titre = intentget.getExtras().getString("titre"); 
     String poster = titre +"="+getTableau(_tlab, iNbLignes, iNbCol); 
     Log.d("reponse","gettableau"); 
     HttpClient client = new DefaultHttpClient(); 
     ResponseHandler<String> res = new BasicResponseHandler(); //newly added line 
     HttpPost post = new HttpPost("http://tweet.envrac.ch/Publication.php"); 
     Log.d("reponse","url"); 
     List<NameValuePair> pairs = new ArrayList<NameValuePair>(1); 
     pairs.add(new BasicNameValuePair("LabColler", poster)); 
     Log.d("reponse","pairs"); 
     post.setEntity(new UrlEncodedFormEntity(pairs)); 
     Log.d("reponse","encode"); 
     //bug ici 
     HttpResponse response = client.execute(post, res); // Changed line 
     Log.d("reponse","post"); 
     return "reponse = "+response; 
    } 
    catch (Exception e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return "erreur"; 
    } 

} 
+0

我可以知道嗎,一旦這段代碼會起作用。謝謝... :) – Aerrow

+0

感謝您的回答,但此代碼仍然沒有工作^^ – Jen

0

HttpResponse response = client.execute(post, new BasicHttpContext()); 

添加此 - >

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
String line = ""; 
while ((line = rd.readLine()) != null) 
{ 
    Log.e("HttpResponse", line); 
} 

此外,您在清單互聯網許可並在您的網址中禁用符號;

+0

感謝您的幫助,但沒有工作:( – Jen

0

這是我曾經的值張貼到它工作正常,如果有可能修改此代碼,按你的服務器的代碼,

public void executeMultipartPost() throws Exception { 
    try { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bm.compress(CompressFormat.JPEG, 75, bos); 
     byte[] data = bos.toByteArray(); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost postRequest = new HttpPost(
       "http://10.0.2.2/cfc/iphoneWebservice.cfc?returnformat=json&amp;method=testUpload"); 
     ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg"); 
     // File file= new File("/mnt/sdcard/forest.png"); 
     // FileBody bin = new FileBody(file); 
     MultipartEntity reqEntity = new MultipartEntity(
       HttpMultipartMode.BROWSER_COMPATIBLE); 
     reqEntity.addPart("uploaded", bab); 
     reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf")); 
     postRequest.setEntity(reqEntity); 
     HttpResponse response = httpClient.execute(postRequest); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       response.getEntity().getContent(), "UTF-8")); 
     String sResponse; 
     StringBuilder s = new StringBuilder(); 

     while ((sResponse = reader.readLine()) != null) { 
      s = s.append(sResponse); 
     } 
     System.out.println("Response: " + s); 
    } catch (Exception e) { 
     // handle exception here 
     Log.e(e.getClass().getName(), e.getMessage()); 
    } 
} 
+0

謝謝我會嘗試以後^ ^ – Jen