2013-05-07 47 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

相关问题