使用HTTPGet android

时间:2015-05-21 08:46:45

标签: android json

我开发了android应用程序,它可以从当前URL加载json: http://test.com/index.php/json/vendor/Lumineux 我想展示其详细供应商,所以我通过了#Lumineux"从我的Android应用程序,所以获取参数是" Lumineux"。但是当我访问这个json URL时遇到麻烦,这里是它的源代码及其错误:

05-21 15:35:02.607: E/AndroidRuntime(29492): FATAL EXCEPTION: AsyncTask #4
05-21 15:35:02.607: E/AndroidRuntime(29492): java.lang.RuntimeException: An error occured while executing doInBackground()
05-21 15:35:02.607: E/AndroidRuntime(29492):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.lang.Thread.run(Thread.java:856)
05-21 15:35:02.607: E/AndroidRuntime(29492): Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 46: http://test.com/index.php/json/vendor/Lumineux
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.net.URI.create(URI.java:727)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at com.example.jiehun.ServiceHandler.makeServiceCall(ServiceHandler.java:71)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at com.example.jiehun.ViewDetailVendor$GetDetail.doInBackground(ViewDetailVendor.java:143)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at com.example.jiehun.ViewDetailVendor$GetDetail.doInBackground(ViewDetailVendor.java:1)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-21 15:35:02.607: E/AndroidRuntime(29492):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-21 15:35:02.607: E/AndroidRuntime(29492):    ... 4 more
05-21 15:35:03.698: E/WindowManager(29492): Activity com.example.jiehun.ViewDetailVendor has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4221a7f0 V.E..... R......D 0,0-456,144} that was originally added here
05-21 15:35:03.698: E/WindowManager(29492): android.view.WindowLeaked: Activity com.example.jiehun.ViewDetailVendor has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{4221a7f0 V.E..... R......D 0,0-456,144} that was originally added here
05-21 15:35:03.698: E/WindowManager(29492):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
05-21 15:35:03.698: E/WindowManager(29492):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
05-21 15:35:03.698: E/WindowManager(29492):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.Dialog.show(Dialog.java:304)
05-21 15:35:03.698: E/WindowManager(29492):     at com.example.jiehun.ViewDetailVendor$GetDetail.onPreExecute(ViewDetailVendor.java:129)
05-21 15:35:03.698: E/WindowManager(29492):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-21 15:35:03.698: E/WindowManager(29492):     at android.os.AsyncTask.execute(AsyncTask.java:534)
05-21 15:35:03.698: E/WindowManager(29492):     at com.example.jiehun.ViewDetailVendor.onCreate(ViewDetailVendor.java:106)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.Activity.performCreate(Activity.java:5117)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2236)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.ActivityThread.access$600(ActivityThread.java:145)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
05-21 15:35:03.698: E/WindowManager(29492):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 15:35:03.698: E/WindowManager(29492):     at android.os.Looper.loop(Looper.java:137)
05-21 15:35:03.698: E/WindowManager(29492):     at android.app.ActivityThread.main(ActivityThread.java:5099)
05-21 15:35:03.698: E/WindowManager(29492):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 15:35:03.698: E/WindowManager(29492):     at java.lang.reflect.Method.invoke(Method.java:511)
05-21 15:35:03.698: E/WindowManager(29492):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:803)
05-21 15:35:03.698: E/WindowManager(29492):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:570)
05-21 15:35:03.698: E/WindowManager(29492):     at dalvik.system.NativeStart.main(Native Method)

这里是我访问json的源代码

        ServiceHandler sh = new ServiceHandler();

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("1", OtherClass.getNamaVendor())); // OtherClass.getNamaVendor()="Lumineux"
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET,params);

这是我的Servicehandler类:

public String makeServiceCall(String url, int method,List<NameValuePair> params) {
        try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }
            httpResponse = httpClient.execute(httpPost);
        } else if (method == GET) {
            // appending params to url

            if (params != null) {
                String paramString=params.get(0).getValue();
                url += "/" + paramString;
                Log.d("urlmmm", url);
            }
            HttpGet httpGet = new HttpGet(url);
            httpResponse = httpClient.execute(httpGet);
        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

    }catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }catch (ClientProtocolException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

1 个答案:

答案 0 :(得分:0)

你需要替换这个

String paramString=params.get(0).getValue();
url += "/" + paramString;enter code here

通过

 String paramString = URLEncodedUtils.format(params, "utf-8");
url += paramString;

希望这会对你有所帮助