gpt4 book ai didi

android - 如何使用 JSON 将数据发布到 Web 服务?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:44 25 4
gpt4 key购买 nike

我必须将以下数据发送到 URl 的网络服务

发送的数据格式为:

new_member=[{"email":"himanshu@avigma.com","username":"himanshu01","pwd":"himanshu01"}]

其中email,usernamepwd 是key,通过edittext string 获取相应的value string。我在单击按钮时发布此数据。

我没有收到任何回复,因为我已经尝试与我的合作开发人员在他的 iphone 相同的应用程序 iphone 版本中进行反核对,因为服务器说用户名和密码无效。

我的 Signup.java 类是:

    Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
EditText e=(EditText)findViewById(R.id.editText1);
String a=e.getText().toString();
EditText e1=(EditText)findViewById(R.id.editText1);
String b=e1.getText().toString();
EditText e2=(EditText)findViewById(R.id.editText1);
String c=e2.getText().toString();
public void onClick(View v) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try{
HttpPost post = new HttpPost("URL");
//System.out.println(post);
json.put("email", a);
json.put("username", b);
json.put("pwd",c);
StringEntity se = new StringEntity( "JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
//System.out.println(response);
}
}
catch(Exception e){
e.printStackTrace();
System.out.println(e.toString());
//createDialog("Error", "Cannot Estabilish Connection");
}
}

}
);

请帮助我,我是 JSON 和 Android.Thanx 解析方面的新手。

最佳答案

这是例子

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"https://api.dailymile.com/entries.json?oauth_token="
+ token);

httpPost.setHeader("content-type", "application/json");
JSONObject data = new JSONObject();

data.put("message", dailyMilePost.getMessage());
JSONObject workoutData = new JSONObject();
data.put("workout", workoutData);
workoutData.put("activity_type", dailyMilePost.getActivityType());
workoutData.put("completed_at", dailyMilePost.getCompletedAt());
JSONObject distanceData = new JSONObject();
workoutData.put("distance", distanceData);
distanceData.put("value", dailyMilePost.getDistanceValue());
distanceData.put("units", dailyMilePost.getDistanceUnits());
workoutData.put("duration", dailyMilePost.getDurationInSeconds());
workoutData.put("title", dailyMilePost.getTitle());
workoutData.put("felt", dailyMilePost.getFelt());

StringEntity entity = new StringEntity(data.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost);

查看此博客的完整信息 http://simpleprogrammer.com/2011/06/04/oauth-and-rest-in-android-part-2/

关于android - 如何使用 JSON 将数据发布到 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8520011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com