在我的应用程序中,有一个愿望清单,用户可以通过手机添加一些带有姓名,备注和图像的心愿单项目。我想将这些数据发送到DB的服务器端。 格式低于..
{
"post": {
"product name": "somename",
"note": "description of the product",
"image": "http://localhost/someimage.jpg",
},
}
所以我的问题是如何构建这个JSON数组并将其POST到服务器数据库... 以及如何编写php以从客户端接收POST请求? 提前谢谢....
答案 0 :(得分:1)
上课:
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;
}
}
在()
:
mainActivity
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()
函数,无论您希望成功完成任何帖子。
制作另一个课程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();
}
}
}
答案 1 :(得分: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();