2016-07-11 5 views
1

私はwebサービスの呼び出しを行うためのiosコードを開発しており、応答がうまく与えられているという点でアプリケーションで作業しています。同じwebserviceコールがアンドロイドを呼び出そうとしています。それは私の応答を与えていない、私は私の両方のコードを投稿していない動作していない誰も私はそれを把握することができますか?上記のコードでアンドロイドコードのwebserviceの応答を取得していません

IOSコード

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",k_SERVER_BASE_ADDRESS,service_name]]; 
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config]; 

    // 2 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
    request.HTTPMethod = @"POST"; 

    [request setValue:[NSString stringWithFormat:@"%@",k_CONTENT_TYPE] forHTTPHeaderField:@"Content-Type"]; 

    // 3 
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 
    [dictionary setObject:[NSString stringWithFormat:@"%@",k_USER_NAME] forKey:@"APIUserName"]; 
    [dictionary setObject:[NSString stringWithFormat:@"%@",k_PASSWORD] forKey:@"Password"]; 

    NSLog(@"%@",dictionary); 
    NSError *error = nil; 
    NSData *data_prm = [NSJSONSerialization dataWithJSONObject:dictionary 
                 options:kNilOptions error:&error]; 

    NSLog(@"%@",[[NSString alloc] initWithData:data_prm encoding:NSUTF8StringEncoding]); 
    if (!error) { 
     // 4 
     NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request 
                    fromData:data_prm completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) { 
                     // Handle response here.. 

                     NSLog(@"this is data : %@",data); 

Androidのコード

public class GETCONTESTANTS extends AsyncTask<Void, Void, Void> { 

    StringEntity se; 
     @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(StartActivity.this); 
     pDialog.setMessage("Loading..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
     // stateList.clear(); 


    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     HttpClient httpClient = new DefaultHttpClient(); 


     HttpPost httpPost = new HttpPost("http://www.fansplay.com/wsfptb20/FBS.svc/GetContestants"); 
      String json = ""; 
      try { 
      // 3. build jsonObject 
      JSONObject jsonObject = new JSONObject(); 
      jsonObject.accumulate("APIUserName", "[email protected]"); 
      jsonObject.accumulate("country","8GB4HE1C-EFSD-4L17-VY2D-A27OC8C52F6M"); 
        // 4. convert JSONObject to JSON to String 
      json = jsonObject.toString(); 

      // ** Alternative way to convert Person object to JSON string usin Jackson Lib 
      // ObjectMapper mapper = new ObjectMapper(); 
      // json = mapper.writeValueAsString(person); 

      // 5. set json to StringEntity 


      se = new StringEntity(json); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

      // 6. set httpPost Entity 
      httpPost.setEntity(se); 

      // 7. Set some headers to inform server about the type of the content 
      httpPost.setHeader("Accept", "application/json"); 
      httpPost.setHeader("Content-type", "application/json"); 

     try { 

      HttpResponse httpResponse = httpClient.execute(httpPost); 

      // 9. receive response as inputStream 
      inputStream = httpResponse.getEntity().getContent(); 

"iOSのコードは、" 正常に動作し、 "アンドロイドコード" 動作していませんさ。誰でも助けてくれますか?

+0

デバッグ、コード役に立つとlogcat出力 –

+0

は、あなたが任意のエラー/例外を取得している投稿してみてみては? –

+0

正確に何がエラーになっていますか? – PriyankaChauhan

答えて

1

は、このコードのスニペットが

public class JSONParser { 
    static InputStream is = null; 
    static JSONObject jobj = null; 
    static String json = ""; 
    String url="http://www.fansplay.com/wsfptb20/FBS.svc/GetContestants"; 

    public JSONParser(){ 

    } 


    public JSONObject makeHttpRequest(String url){ 

    JSONObject jo = null; 
    jo = new JSONObject(); 

    try { 
     jo.accumulate("APIUserName", "[email protected]"); 
     jo.accumulate("country","8GB4HE1C-EFSD-4L17-VY2D-A27OC8C52F6M"); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    JSONObject job=new JSONObject(); 
    try { 
     job.accumulate("aloha", jo); 
    } catch (JSONException e2) { 
     // TODO Auto-generated catch block 
     e2.printStackTrace(); 
    } 

    Log.e("url", job.toString()); 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(this.url); 
     try { 
      httppost.setEntity(new StringEntity(job.toString(), "UTF- 8")); 

      //    httppost.toString(); 
     //    Log.e("url", httppost.toString()); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     try { 
      HttpResponse httpresponse = httpclient.execute(httppost); 
      HttpEntity httpentity = httpresponse.getEntity(); 
      is = httpentity.getContent(); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      try { 
       while((line = reader.readLine())!=null){ 
        sb.append(line+"\n"); 

       } 
       is.close(); 
       json = sb.toString(); 
       Log.e("url", json); 
       try { 
        jobj = new JSONObject(json); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    return jobj; 

    } 
+0

無効なパスワードのようなRestAPI->レスポンスを試しました – Sreekanth

+0

ツールでそれをチェックしましたか?それともこのクラスを呼び出すのですか? –

+0

これを試してください - > JSONParserオブジェクト=新しいJSONParser(); object.makeHttpRequest( "http://www.fansplay.com/wsfptb20/FBS.svc/GetContestants"); – Sreekanth

関連する問題