-1

每当我调用网络服务: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();
}

手机代码:

  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 文件以检索响应。所以你必须使用 Http post 方法来做到这一点。尝试以下代码以向服务器发送值:

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 回答