如何正确制作一个Android HttpGet请求

时间:2014-05-07 23:15:54

标签: android django

我正在尝试从android到后端Django服务器发出get请求。每次发出请求时都会抛出异常,并且服务器不显示它已被联系。我该怎么做呢?感谢。

这是我的尝试:

客户端android:

try{

        URI website = new URI(URL);
        //Request to server
        HttpGet httpget = new HttpGet();
        httpget.setURI(website);
        HttpResponse response = Client.execute(httpget);
        Log.d("EXECUTED",response.toString());
    }catch (Exception ex){
        textView.setText("Fail");
    }

服务器端Django:

def app(request, id):
      context = RequestContext(request)

      fab_categories = Catagories.objects.all()

      return HttpResponse("worked")

按要求这是logcat:

05-07 22:09:35.150: W/System.err(1624):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-07 22:09:35.150: W/System.err(1624):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-07 22:09:35.150: W/System.err(1624):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-07 22:09:35.150: W/System.err(1624):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-07 22:09:35.150: W/System.err(1624):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-07 22:09:35.150: W/System.err(1624):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-07 22:09:35.150: W/System.err(1624):     at com.companion.app.MainActivity2.onCreate(MainActivity2.java:57)
05-07 22:09:35.150: W/System.err(1624):     at android.app.Activity.performCreate(Activity.java:5240)
05-07 22:09:35.150: W/System.err(1624):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-07 22:09:35.150: W/System.err(1624):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
05-07 22:09:35.150: W/System.err(1624):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
05-07 22:09:35.150: W/System.err(1624):     at android.app.ActivityThread.access$800(ActivityThread.java:145)
05-07 22:09:35.150: W/System.err(1624):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
05-07 22:09:35.150: W/System.err(1624):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-07 22:09:35.150: W/System.err(1624):     at android.os.Looper.loop(Looper.java:136)
05-07 22:09:35.150: W/System.err(1624):     at android.app.ActivityThread.main(ActivityThread.java:5081)
05-07 22:09:35.150: W/System.err(1624):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 22:09:35.150: W/System.err(1624):     at java.lang.reflect.Method.invoke(Method.java:515)
05-07 22:09:35.150: W/System.err(1624):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
05-07 22:09:35.150: W/System.err(1624):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-07 22:09:35.150: W/System.err(1624):     at dalvik.system.NativeStart.main(Native Method)

此外,我已向清单添加了相应的权限。

1 个答案:

答案 0 :(得分:0)

嗨那里:)我正在开发一个类似的应用程序,我按照本教程。

很抱歉只是在这里删除一个链接,但这是非常自我解释的,代码记录很清楚。

Android Hive TUTORIAL: HTTP

忽略标题,查看InboxActivity.java代码