0

我在 byethost.com 上的 php 中编写了我的 web 服务,它在浏览器和“ http://www.jsoneditoronline.org/ ”上运行良好。但是当我从 android 应用程序调用服务时,它会出错。

05-06 00:31:34.475: E/AndroidRuntime(24246): java.lang.IllegalArgumentException: 主机名不能为空 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): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:519) 05-06 00:31:34.475: E/AndroidRuntime(24246): 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509) 05-06 00:31:34.475: E/AndroidRuntime(24246):在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 05-06 00:31:34.475: E/AndroidRuntime(24246): 在 com.shreeji.jsonparser.JSONParser.getJSONFromUrl(JSONParser. java:47) 05-06 00:31:34.475: E/AndroidRuntime(24246): 在 apidatahandler.TilesApiCall.getTilesDetails(TilesApiCall.java:23) 05-06 00:31:34.475: E/AndroidRuntime(24246): 在 com.shreejitiles.android.Ui_MainPage$1.run(Ui_MainPage.java:41) 05-06 00:31:34.475: E/AndroidRuntime(24246 ): 在 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;

    }
}
4

1 回答 1

0

这是因为您没有为您的请求设置主机 url。

于 2013-05-05T19:23:29.793 回答