这是我的unsplash API的HTTP请求代码 在浏览器中打开时HTTP运行成功,但在java中的HTTP请求中,我有这个问题
我如何解决这个问题
注意:此代码使用Instagram API成功运行
new Thread() {
@Override
public void run() {
Log.i("ttttttrrrraaaaiiiinnn", "Getting access token");
try {
URL url = new URL("https://api.unsplash.com/photos?client_id=***");
Log.i("", "Send GET url liked image" + url.toString());
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
OutputStreamWriter writer = new OutputStreamWriter(
urlConnection.getOutputStream());
writer.write("url");
writer.flush();
String response = Utils.streamToString(urlConnection
.getInputStream());
Log.i("", "*?*?*?*?*?*?*?*?*?*?*? response *?*?*?*?*?*?*?*?*?*?*?" + response);
JSONObject jsonObj = (JSONObject) new JSONTokener(response)
.nextValue();
tv.setText(jsonObj.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}.start();
错误
java.io.FileNotFoundException: https://api.unsplash.com/photos?client_id=****
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:260)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
W/System.err: at com.ksucs.graduationproject.myinterests.MainActivity$1.run(MainActivity.java:97)
答案 0 :(得分:0)
restful api只支持GET
访问,你应该改变请求的方法/删除setRequestMethod(...)的行。你不能写一些东西到服务器,所以你必须删除out
变量和相对语句行。
urlConnection.setRequestMethod("GET");//by default is GET