如何发布数据,使用JSON一个web服务? [英] How to post data to a Webservice using JSON?

查看:88
本文介绍了如何发布数据,使用JSON一个web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数据,其URL发送到Web服务

I have to send the following data to webservice at URl

要发送数据的格式为:

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

其中,电子邮件用户名 PWD 是键和具有相应的字符串值,通过字符串的EditText获取。我张贴在点击按钮的这一数据。

where email,username and pwd is the key and has corresponding value string fetched through edittext string. I am posting this data on click of button.

我没有得到任何回应bcoz我试图反击连带着我的iPhone相同的应用程序iPhone版的用户名和密码共同开发检查,这是无效的由服务器说。

I am not getting any response bcoz i have tried to counter check this with my co developer in his iphone same app iphone version as the username and password is not valid is said by server.

我的 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。

Please assist me i am a newbie in JSON and parsing in 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);

看到这个博客<一个完整的信息href="http://simpleprogrammer.com/2011/06/04/oauth-and-rest-in-android-part-2/">http://simpleprogrammer.com/2011/06/04/oauth-and-rest-in-android-part-2/

这篇关于如何发布数据,使用JSON一个web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆