gpt4 book ai didi

java - Android Rest Api Post 400 错误请求

转载 作者:行者123 更新时间:2023-12-01 06:13:33 27 4
gpt4 key购买 nike

我正在开发一个应用程序,它将向服务器发送密码以检查其是否正确,服务器将返回代表不同状态的0,1,2,3我不确定这个 400 错误请求是从哪里来的。我已经调用服务器并收到了 json 文件,但我怀疑这就是我解析它的方式?非常感谢任何帮助。

更新

更新。我意识到这里的部分错误是我试图发送一个字符串而不先将其转换为 JSON 文件。这就是为什么我一直收到“status:0”,它代表发生了错误。我已将字符串转换为包含字符串变量的 RegisterRequest 对象。现在我收到状态 2,它代表密码不正确。然而我的错误请求400仍然存在。请任何帮助或建议对我非常有帮助

@Headers("Content-Type: application/json")
@POST("removed/{id}")
RegisterResponse registerEvent(
@Path("id") int ID,
@Body RegisterRequest password
) throws NetworkException;

RegisterResponse 类:

public class RegisterResponse {
public String status;
}

RegisterRequest 类

public class RegisterRequest {

String passcode ;

public RegisterRequest(String passcode)
{
this.passcode=passcode;
}
}

调用服务器:

try {
RegisterResponse response = NetworkHelper.makeRequestAdapter(this)
.create(testApi.class).register(ID, pass);

if(response.status=="1"||response.status=="3")
{
return true;
}
else
return false;
} catch (NetworkException e) {
e.printStackTrace();
}
return false;
}

创建请求适配器

private static RestAdapter.Builder makeRequestAdapterBuilder(final Context context, ErrorHandler errorHandler, String dateTimeFormat)
{
RequestInterceptor requestInterceptor = new RequestInterceptor()
{
@Override
public void intercept(RequestInterceptor.RequestFacade request)
{
request.addHeader("Authorization", AppPrefs.getInstance(context).getSessionToken());
}
};

Gson gson = new GsonBuilder().setDateFormat(dateTimeFormat).create();
GsonConverter gsonConverter = new GsonConverter(gson);

RestAdapter.Builder builder = new RestAdapter.Builder()
.setRequestInterceptor(requestInterceptor).setConverter(gsonConverter)
.setLogLevel(RestAdapter.LogLevel.FULL).setLog(new AndroidLog("Boom!"))
.setEndpoint(BASE_URL);

if(errorHandler != null)
{
builder.setErrorHandler(errorHandler);
}

if(instance == null)
{
instance = new OkClient(makeTimeoutClient(READ_TIMEOUT, CONNECT_TIMEOUT));
}
builder.setClient(instance);

return builder;
}

错误代码:

D/Boom!﹕ ---> HTTP POST https://serverurl/29520
D/Boom!﹕ Authorization: Token 4a712131231299ba5bd0737163bcf1d6ff5325b4ef
D/Boom!﹕ Content-Type: application/json
D/Boom!﹕ Content-Length: 17
D/Boom!﹕ {"passcode":"55"}
D/Boom!﹕ ---> END HTTP (17-byte body)
D/dalvikvm﹕ GC_FOR_ALLOC freed 398K, 5% free 10098K/10528K, paused 19ms, total 19ms
W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
I/dalvikvm﹕ Could not find method java.nio.file.Files.newOutputStream, referenced from method okio.Okio.sink
W/dalvikvm﹕ VFY: unable to resolve static method 29588: Ljava/nio/file/Files;.newOutputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/OutputStream;
D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljava/nio/file/Path;)
W/dalvikvm﹕ VFY: unable to find class referenced in signature ([Ljava/nio/file/OpenOption;)
I/dalvikvm﹕ Could not find method java.nio.file.Files.newInputStream, referenced from method okio.Okio.source
W/dalvikvm﹕ VFY: unable to resolve static method 29587: Ljava/nio/file/Files;.newInputStream (Ljava/nio/file/Path;[Ljava/nio/file/OpenOption;)Ljava/io/InputStream;
D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x000a
D/Boom!﹕ <--- HTTP 400 https://serverurl/29520 (489ms)
D/Boom!﹕ Allow: POST, OPTIONS
D/Boom!﹕ Cache-control: no-cache="set-cookie"
D/Boom!﹕ Content-Type: application/json
D/Boom!﹕ Date: Sat, 25 Apr 2015 14:19:12 GMT
D/Boom!﹕ Server: Apache/2.4.7 (Ubuntu)
D/Boom!﹕ Set-Cookie: AWSELB=B19BD105182D026362B8BC00141D5791F1205A08CAA1F992D8F8EC9B69BB363D3418C9476B70A93F8D096B34C3D75087DEEC69EB5C6D2C30536BE30BC1E0A0EBB500881D9D;PATH=/;MAX-AGE=60
D/Boom!﹕ Vary: Accept,Cookie
D/Boom!﹕ X-Frame-Options: SAMEORIGIN
D/Boom!﹕ Content-Length: 15
D/Boom!﹕ Connection: keep-alive
D/Boom!﹕ OkHttp-Selected-Protocol: http/1.1
D/Boom!﹕ OkHttp-Sent-Millis: 1429971549555
D/Boom!﹕ OkHttp-Received-Millis: 1429971549598
D/Boom!﹕ {"status": "2"}
D/Boom!﹕ <--- END HTTP (15-byte body)
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415c7ba8)
D/dalvikvm﹕ GC_FOR_ALLOC freed 439K, 5% free 10173K/10644K, paused 20ms, total 20ms
D/dalvikvm﹕ GC_FOR_ALLOC freed 494K, 5% free 10187K/10716K, paused 20ms, total 20ms
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: AndroidCall , PID: 5922
retrofit.RetrofitError: 400 BAD REQUEST
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:388)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy2.registerEvent(Native Method)
at AndroidCall .app.ui.activities.EventListActivity.matchPasscode(ListActivity.java:387)
at AndroidCall .app.ui.activities.EventListActivity.access$200(ListActivity.java:49)
at AndroidCall .app.ui.activities.EventListActivity$6.onClick(ListActivity.java:302)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
I/Process﹕ Sending signal. PID: 5922 SIG: 9

最佳答案

我已经弄清楚了。我正在处理错误的异常,并且 API 返回 400 而不是 401,因为发送了错误的密码。我已将对服务器网络异常错误的调用更改为异常错误。

感谢大家的帮助。

关于java - Android Rest Api Post 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29860752/

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