制作这个类:
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();
}
}
}