gpt4 book ai didi

java - 使用 HTTPUrlConnection 的 RESTful 请求

转载 作者:行者123 更新时间:2023-12-02 06:30:53 26 4
gpt4 key购买 nike

我正在编写我的第一个 Android 应用程序,并尝试掌握从应用程序内进行 REST 调用的窍门。在几页和其他 SO 帖子之后,我得到了这个:

URL serverUrl = new URL("http://localhost:13980/api/maps/"); //the value is hardcoded for testing purposes

public String getMapsJson()
{
Log.d("Minimap", ">> getting maps json from " + serverUrl.toString());
String output = "";

HttpURLConnection connection = null;

try
{
connection = (HttpURLConnection) serverUrl.openConnection();
connection.setRequestMethod("GET");
Log.d("Minimap", "connection opened!");
InputStream in = new BufferedInputStream(connection.getInputStream());
Log.d("Minimap", "input stream captured");
output = readStream(in);
Log.d("Minimap", "stream read");
}

catch (Exception e)
{
Log.d("Minimap", e.getMessage()); //************* This is line 53 <<<<<<
}

finally
{
connection.disconnect();
}


Log.d("Minimap", "JSON: " + output);
return output;
}

private String readStream(InputStream in)
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
String output = line;

while((line = reader.readLine()) != null)
output += line;

return output;
}
catch (IOException e)
{
e.printStackTrace();
return "";
}
}

我从 logcat 得到的输出只是

11-19 07:38:31.599    3813-3813/com.northstar.minimap D/Minimap﹕ >> getting maps json from http://localhost:13980/api/maps/
11-19 07:38:31.599 3813-3813/com.northstar.minimap D/Minimap﹕ connection opened!

然后应用程序崩溃并显示以下堆栈跟踪:

11-19 08:02:50.903      744-744/com.northstar.minimap E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.northstar.minimap/com.northstar.minimap.MapActivity}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:138)
at com.northstar.minimap.Communicator.getMapsJson(Communicator.java:53)
at com.northstar.minimap.MapActivity.onCreate(MapActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
11-19 08:02:50.933 308-445/system_process W/ActivityManager﹕ Force finishing activity com.northstar.minimap/.MapActivity
11-19 08:02:50.943 308-445/system_process W/ActivityManager﹕ Force finishing activity com.northstar.minimap/.MainActivity

它正在查询的页面 (http://localhost:13980/api/maps/) 是一个简单的 GET 端点,它会返回一些 JSON。此时,如果它拉出的 JSON 被打印到 LogCat,我会很高兴。

谢谢!

最佳答案

您的 localhost 可能有误,服务器未在 Android 设备或模拟器本身上运行。

您应该使用 10.0.2.2 访问运行模拟器的计算机,请参阅 http://developer.android.com/tools/devices/emulator.html

或者网络服务器的IP。 (首先确保其可以被外界访问)

正如 @Jason C 所说,你的错误处理非常糟糕。你不应该捕获所有异常。

一个相当标准的方法是让您的方法抛出 IOException,因此调用 getMapsJson 的代码可以处理这个特定且常见的异常(例如,无连接)。

如果您想处理 HTTP 错误和错误代码,您应该在打开 InputStream 之前检查结果代码。如果发生错误,如果除了已知结果代码之外您还需要实际响应,您还可以获得 ErrorStream 而不是 InputStream

关于java - 使用 HTTPUrlConnection 的 RESTful 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com