我想使用OKhttp3进行POST,但它始终返回404.我的代码有什么问题吗?服务器端不断告诉我url和Json是正确的.
我尝试了另一个发布网址和Json,它可以正常工作,返回码200.
package com.epsfamily.www.kaipostb;import androID.os.AsyncTask;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import java.io.IOException;import java.util.logging.Logger;import okhttp3.MediaType;import okhttp3.OkhttpClIEnt;import okhttp3.Request;import okhttp3.Requestbody;import okhttp3.Response;public class MainActivity extends AppCompatActivity { OkhttpClIEnt clIEnt = new OkhttpClIEnt(); public static final MediaType JsON = MediaType.parse("application/Json; charset=utf-8"); String url; // test data String bowlingJson(String player1, String player2) { return "{ \"deviceid\": \"1abcd\", \"buildingID\": 2, \"isRoutingOptimized\": false, \"isFromCurrentLocation\": true, \"startPoint\": 1, \"destinations\": [ 3, 4 ], \"createdAt\": \"2016-02-19T20:08:45.308Z\" } "; } String doPostRequest(String url, String Json) throws IOException { com.orhanobut.logger.Logger.Json(Json); Requestbody body = Requestbody.create(JsON, Json); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = clIEnt.newCall(request).execute(); com.orhanobut.logger.Logger.v("code: " + response.code()); return response.body().string(); } @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); url = "http://54.86.18.109:5080/Routing"; new PostAsync().execute(); } public class PostAsync extends AsyncTask<String, VoID, String> { @OverrIDe protected String doInBackground(String... params) { String Json = bowlingJson("Jesse", "Jake"); String postResponse = null; try { postResponse = doPostRequest(url, Json); } catch (IOException e) { e.printstacktrace(); } System.out.println("res: " + postResponse); return postResponse; } }}
解决方法:
去这里:http://54.86.18.109:5080/Routing
你得到什么?是404.再次检查网址.
编辑:好的,它是POST,但是我仍然可以通过POST请求获得404.
总结以上是内存溢出为你收集整理的android-OKHttp3发布方法不断返回404错误全部内容,希望文章能够帮你解决android-OKHttp3发布方法不断返回404错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)