我的网络服务是在byethost.com托管的php中写的 它在浏览器和“http://www.jsoneditoronline.org/”上运行良好。 但是当我从Android应用程序调用服务时它会出错。
05-06 00:31:34.475:E / AndroidRuntime(24246):java.lang.IllegalArgumentException:主机名不能为null 05-06 00:31:34.475:E / AndroidRuntime(24246):at org.apache.http.HttpHost。(HttpHost.java:83) 05-06 00:31:34.475:E / AndroidRuntime(24246):在org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:519) 05-06 00:31:34.475:E / AndroidRuntime(24246):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509) 05-06 00:31:34.475:E / AndroidRuntime(24246):at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 05-06 00:31:34.475:E / AndroidRuntime(24246):at com.shreeji.jsonparser.JSONParser.getJSONFromUrl(JSONParser.java:47) 05-06 00:31:34.475:E / AndroidRuntime(24246):at apidatahandler.TilesApiCall.getTilesDetails(TilesApiCall.java:23) 05-06 00:31:34.475:E / AndroidRuntime(24246):at com.shreejitiles.android.Ui_MainPage $ 1.run(Ui_MainPage.java:41) 05-06 00:31:34.475:E / AndroidRuntime(24246):at java.lang.Thread.run(Thread.java:856)
请帮帮我。
我用来解析来自服务器的响应的代码。
package com.shreeji.jsonparser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params)
throws ClientProtocolException, IOException, JSONException {
Log.i("url", url);
// Making HTTP request
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
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();
jObj = new JSONObject(json);
// return JSON String
return jObj;
}
}
答案 0 :(得分:0)
因为您没有为您的请求设置主机网址。