Android-如何将数据发布到服务器数据库
在我的应用程序中,有一个愿望列表,用户可以从手机中添加一些带有姓名、备注和图像的愿望列表项目。我想把这些数据发送到DB的服务器端。 格式如下Android-如何将数据发布到服务器数据库,android,parsing,client-server,http-post,Android,Parsing,Client Server,Http Post,在我的应用程序中,有一个愿望列表,用户可以从手机中添加一些带有姓名、备注和图像的愿望列表项目。我想把这些数据发送到DB的服务器端。 格式如下 { "post": { "product name": "somename", "note": "description of the product", "image": "http://localhost/someimage.jpg", }, } 所以我的问题是如何形成这个JSON数组并将其发布到服务器DB。。。
{
"post": {
"product name": "somename",
"note": "description of the product",
"image": "http://localhost/someimage.jpg",
},
}
所以我的问题是如何形成这个JSON数组并将其发布到服务器DB。。。
如何编写php代码来接收来自客户端的POST请求?
提前感谢……试试这个
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();
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://yourwebsite.com.au/index.jsp");
String trackid=getTrackid();
List namevaluepairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“columval1”、“123”);
添加(新的BasicNameValuePair(“columval2”、“145”);
添加(新的BasicNameValuePair(“columval3”、“445”);
setEntity(新的UrlEncodedFormEntity(namevaluepairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity rp=response.getEntity();
String OrigResponsetText=EntityUtils.toString(rp);
字符串htmlTextStr=Html.fromHtml(origressponsetext.toString();
pass=htmlTextStr.trim();
制作此类课程:
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;
}
}
现在调用Fun()
创建另一个类,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();
}
}
}
服务器后端必须使用PHP吗?不!还有其他选择吗?你可以使用Java和Google应用程序引擎。在Eclipse中,你创建了一个新的“谷歌应用引擎连接的Android应用程序”,它将创建应用程序和服务器后端。然后,您可以在服务器上添加实体,并从IDE生成android客户端库。它是RESTful和安全的。在任何需要的地方编写…例如,当您单击一个按钮时,如果您想向远程数据库插入一些数据,请在按钮内编写相同的代码。单击Action和..我想我还需要在客户端编写json解析器..对吗?非常感谢!!我太初学了,所以我有个问题。我应该在哪里创建http类和postdata类?我需要为此创建其他java文件吗?我在主活动中添加了乐趣。你必须在任何包中创建httpclass.java文件和postdata.java文件,并在主活动中调用乐趣。我不知道你想在按钮单击或加载时发布数据。所以uhav到jst调用乐趣,无论你喜欢什么好。。。
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();
}
}
}