2013-05-07 46 views
-1

在我的應用程序中,有一個願望清單,用戶可以添加一些願望清單項目的名稱,備註和手機中的圖像。我想將這些數據發送到數據庫的服務器端。 格式如下..Android - 如何POST數據到服務器數據庫

{ 
    "post": { 
    "product name": "somename", 
    "note": "description of the product", 
    "image": "http://localhost/someimage.jpg", 
    }, 
} 

所以我的問題是我如何能夠形成這個JSON數組,然後發佈到服務器數據庫... 以及如何編寫PHP來接收來自客戶端的POST請求? 在此先感謝....

+0

服務器是否後端必須在PHP? – 2013-05-07 13:49:14

+0

不!其他選擇可能是什麼? – 2013-05-07 14:00:52

+0

您可以使用Java和Google App Engine。在Eclipse中,您創建了一個新的「Google App Engine連接的Android應用程序」,它將使應用程序和服務器後端。然後,您可以在服務器上添加實體,並從IDE生成Android客戶端庫。它是RESTful和安全的。 – 2013-05-07 14:04:13

回答

1

產生這個類:在你的mainActivity

public class HttpClass 
{ 
public static String postData(String url,List<NameValuePair> params) { 
    // Create a new HttpClient and Post Header 

    String responseString = ""; 
    String responsemsg = ""; 

    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     //String tempUrl = HungryPagesConfig.registrationAPI; 
     HttpPost httppost = new HttpPost(url); 
     httppost.setEntity(new UrlEncodedFormEntity(params)); 
     HttpResponse response = httpclient.execute(httppost); 
     responseString = EntityUtils.toString(response.getEntity()); 
     // Log.e("Rsponse", EntityUtils.toString(response.getEntity())); 

     Log.e("Rsponse", responseString); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return responseString; 
} 
} 

做專()

public void Fun() 

{ 
JSONObject Json,Mainjson; 
String Data; 
try { 
      Json.put("product name", "somename"); 
      Json.put("note", "description of the product"); 
      Json.put("image","http://localhost/someimage.jpg"); 
      Mainjson.put("post",Json); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Data = Mainjson.toString(); 
     Log.e("Rsponse", Data); 

     PostData.nameValuePairs = new ArrayList<NameValuePair>(); 
     PostData.add(new BasicNameValuePair("data", Data)); 
} 

現在所說的Fun()發揮你想要的職位的任何功能o成功完成。

讓另一個類,PostData

public class PostData 
{ 

String url; 
JSONObject add; 
public HttpClass jParser = new HttpClass(); 
public void post() 
{ 
tempUrl = HungryPagesConfig.AddMenuItemAdmin; 
     try { 
      add = new JSONObject(jParser.postData(url, 
        nameValuePairs)); 
      Log.e("Rsponse", add.toString()); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

} 
} 
+0

非常感謝!我是一個太初學者..所以我有一個問題。我應該在哪裏製作http類和postdata類?..我需要爲其製作其他java文件嗎?我在主要活動中增添了樂趣。 – 2013-05-07 14:09:45

+0

你必須創建httpclass.java文件和postdata.java文件在任何包裝和nd樂趣調用你的主要activity.i dnt knw你想發佈的數據按鈕點擊或load.so uhav jst調用的樂趣什麼你喜歡okk ... – ishu 2013-05-08 05:58:43

0

試試這個

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://yourwebsite.com.au/index.jsp"); 
String trackid= getTrackid(); 
List<NameValuePair> namevaluepairs = new ArrayList<NameValuePair>(2); 
namevaluepairs.add(new BasicNameValuePair("columval1","123")); 
namevaluepairs.add(new BasicNameValuePair("columval2","145")); 
namevaluepairs.add(new BasicNameValuePair("columval3","445")); 
httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs)); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity rp = response.getEntity(); 
String origresponseText = EntityUtils.toString(rp); 
String htmlTextStr = Html.fromHtml(origresponseText).toString(); 
pass=htmlTextStr.trim(); 
+0

Thx !!順便說一句,我應該在哪裏添加此代碼在我的應用程序? – 2013-05-07 13:57:48

+0

寫任何你需要的地方....例如,當你點擊一個按鈕,如果你想插入一些數據到遠程數據庫在按鈕點擊動作 – 2013-05-07 14:00:49

+0

和..我想我需要編碼JSON解析器客戶端也... ..? – 2013-05-07 14:04:39

相關問題