-1

Webサービス( http://www.grubolympx.com/app/rating.php )を呼び出すたびに、「適切な値を送信してください」というメッセージが送信されます。

データを取得するために、このWebサービスに適切な値を渡すことを考えてください。

これは私のコードです:

public String readFeed() {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    //HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json");
    //HttpGet httpGet = new HttpGet("http://ragecomics.cloudapp.net/RestService.svc/json/Hot/0");

    HttpGet httpGet = new HttpGet("http://www.grubolympx.com/app/rating.php?q=India");
    httpGet.setHeader("content-type", "application/php");
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {

             HttpResponse response1 = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } else {
            Log.e(AdcActivity.class.toString(), "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}

iPhoneコード:

  NSURL *url = [NSURL URLWithString:@"grubolympx.com/app/rating.php"]; 
  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  [request setRequestMethod:@"POST"]; 
  [request addRequestHeader:@"User-Agent" value:@"ASIHTTPRequest"]; 
  [request addRequestHeader:@"Content-Type" value:@"application/json"]; 
  [request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]; 
  [request setDelegate:self]; 
  [request startAsynchronous]; } 
–

(void)requestFinished:(ASIHTTPRequest *)request { 
  NSString *responseString = [request responseString]; 
  NSDictionary *responseDict = [responseString JSONValue]; 
  NSLog(@"%@",responseString); 
  if ([responseString isEqualToString:@"SUCCESS"]) { 
    // Display succeed msg 
    [Appirater appLaunched]; 
  } else{ NSLog(@"%@",responseString); } 
– 

else if (responseDict != NULL){ 
  NSMutableDictionary *rateDictionary = [[[NSMutableDictionary alloc] init] autorelease]; 
  [rateDictionary setObject:[responseDict valueForKey:@"dishes"] forKey:@"Foodname"];
  [rateDictionary setObject:[responseDict valueForKey:@"ratings"] forKey:@"rating"];
  [rateDictionary setObject:[responseDict valueForKey:@"country"] forKey:@"country"];
  [self updateAverageRateWith:rateDictionary]; 
  // Update average rate in rateArray. 
}
4

3 に答える 3

4

私があなたの質問から理解した限り、あなたは応答を取得するためにサーバー上のphpファイルにデータを送信しようとしています。したがって、これを行うにはHttppostメソッドを使用する必要があります。サーバーに値を送信するには、次のコードを試してください。

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url); // url = webpage u send data with request
httpPost.setEntity(new UrlEncodedFormEntity(params)); //params is a List<NameValuePair> where you store your value to send server

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent(); //is is an InputStream

応答を取得するには、次のコードを使用します。

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

その後、サーバーから取得したjsonを解析し、データを収集できます。

于 2012-08-10T07:39:07.733 に答える
1

サービスを開発した人からサービスが機能していることを確認してください。また、正しい応答を得るために渡されるパラメータを彼に依頼してください。

于 2012-08-10T07:43:19.573 に答える
-1

そのURLをWebブラウザーに貼り付けると、同じメッセージで応答します。

PCに接続した状態でアプリを実行すると、log catは何か便利なものを表示しますか?

于 2012-08-10T07:23:22.813 に答える