2011-12-15 21 views
8

私はURLにWebサービスに次のデータを送信する必要がJSONを使用してWebサービスにデータを投稿する方法は?

送信されるデータのフォーマットは:emailusernamepwdキーとのEditText介して取り込まれた対応する値列を有する

new_member=[{"email":"[email protected]","username":"himanshu01","pwd":"himanshu01"}] 

文字列。クリックしてこのデータを投稿しています。

私は彼のアイフォーンと同じアプリの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で解析し、私を支援してください。

答えて

10

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/

+0

返信のためのThanxは私がnew_member =を渡す方法を教えてください。私はURLに追加していますが、実際に提供されるURLはhttp://www.fourerr.com/ws/signupです.jsonとしてnewmemberを取るべきですか?オブジェクト。??私を助けてください。 –

+0

このnew_memberの値は何ですか?トークンのように渡すようにしてください – Pratik

+2

私は使用しないでください** StringEntity entity = new StringEntity(data.toString()); ** ** StringEntity entity = new StringEntity(paramInput.toString()、HTTP.UTF_8 ); ** - 私の場合、これはjson-stringでキリル文字をエンコードする際の問題を解決しました。エンコード用に – Prizoff

2

ブロック括弧はJSON配列を表しているため、jsonオブジェクトをJSONArrayにラップするだけで済みます。

JSONArray array = new JSONArray(); 
JSONObject json = new JSONObject(); 
json.put("email", a); 
json.put("username", b); 
json.put("pwd",c); 
array.put(json); 

エンティティへのput array

重要:メインスレッドで長期実行(ネットワークなど)タスクを実行しないでください。バックグラウンドで長時間実行されるタスクを適切に実行してUIを更新するには、AsyncTaskを使用します。

+0

ありがとうを指定するのを忘れて、私は、URLに付加していますが、実際の提供URLはhttp://www.fourerr.comをあるとして、私はnew_member =を渡す方法を教えてください/ ws/signup私はjsonオブジェクトとしてnewmemberを取るべきですか?私を助けてください.. –

1

まず、のStringsをメソッドのEditTextsから取得する必要があります。あなたはJSONArray、このようなものを作成する必要がありますので、サーバは、ただ一つの項目でJSONArrayを期待よう

そして、それが見えます:

JSONArray jsonArray=new JSONArray(); 
jsonArray.put(json); //your current json 
StringEntity entity=new StringEntity("new_member="+jsonArray); 
ここ
4
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 

nameValuePairs.add(new BasicNameValuePair("email", "[email protected]")); 
nameValuePairs.add(new BasicNameValuePair("username", "himanshu01")); 
nameValuePairs.add(new BasicNameValuePair("pwd", "himanshu01")); 

// You have to add your parameters as nameValuePairs 

String res = ""; 
       try 
       { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("URL"); 

          // Add your data 

          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
          HttpResponse response = httpclient.execute(httppost); 
          res = EntityUtils.toString(response.getEntity()); 
          JSONTokener t = new JSONTokener(res); 
          JSONArray a = new JSONArray(t); 
          JSONObject o = a.getJSONObject(0); 
          String sc = o.getString("success"); 
          if(sc.equals("1")) 
          { 
           // posted successfully 
          } 
else 
{ 
// error occurred 
} 
       } 
       catch (Exception e) 
       { 

        e.printStackTrace(); 
       } 

いけない返事をandroid.permission.INTERNETあなた許可

関連する問題