我在android中发送带有javacode的服务器请求,但我得到的回复代码为414.我是android新手。请帮忙。我正在使用post方法。我使用以下代码:
package com.hfad.evenit;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by Vicky on 22-Jul-15.
*/
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url1) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
int response=0;
// Download JSON data from URL
try {
URL url = new URL(url1);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
response = conn.getResponseCode();
is = new BufferedInputStream(conn.getInputStream());
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
Log.e("log_tag", "Response " + response);
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
我的网址请求是:
jsonObject=JSONfunctions.getJSONfromURL("http://54.169.88.65/events/eventmain/get_users.php?json="+URLencoded);
URLencoded
将json数据转换为字符串然后进行urlencoded。
答案 0 :(得分:2)
首先,正如任何4xx错误代码所暗示的那样,它在客户端有一些事情要做,而不是服务器端。
当您获得414时,意味着您要点击的网址太长。
414 - 请求URL太长Web服务器响应此错误 当它拒绝服务请求时,因为Request-URL是 比服务器愿意或能够解释的更长。这种罕见 条件只有在客户不正确时才会发生 将POST请求转换为具有长查询信息的GET请求, 当客户端进入重定向的URL“黑洞”时 (例如,指向其自身后缀的重定向URL前缀),或 当服务器受到试图利用的客户端的攻击时 某些服务器中存在使用固定长度缓冲区的安全漏洞 阅读或操纵Request-URL。通常是Web服务器设置 对真正的URL的长度有相当大的限制,例如高达2048或 4096个字符。如果长URL有效且您收到414错误, 然后可能需要重新配置Web服务器以允许此类URL 通过
参考:here
这告诉我,你附加到“?json =”结尾的字符串太长了,你可能不应该在URL中传递它。我还可以看到,您正在尝试 POST 请求,甚至认为您在那里的做法与POST无关。您正在使用query string发出GET请求。
因此,根据服务的设计方式,如果它接受了查询字符串,除了确保您可以适应服务器上设置的有效URL限制之外,您还无法做很多事情。
如果该服务被设计为接受POST请求(在这种情况下我应该告诉它真的应该!),那么你应该用一个正文实现一个正确的POST请求:
json = "{your-valid-json-here}"
我建议您查看Google提供的框架,名为Volley并尝试在AsyncTask中实施(如果需要),以便能够对响应采取行动:
<强> MainActivity.java 强>
package me.monori.examples.post.activities;
import android.app.Activity;
import android.os.Bundle;
import me.monori.examples.post;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// This is where you call your service
new PostExampleAsync(this).execute();
}
//...//
}
<强> PostExampleAsync.java 强>
package me.monori.examples.post;
import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import me.monori.examples.post.interfaces.onServiceCallCompleted;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Antal Janos Monori on 10-05-2015.
* Copyright 2015 monori.me. All rights reserved.
*/
public class PostExampleAsync extends AsyncTask<String, Boolean, Boolean> implements onServiceCallCompleted
{
private static final String TAG = "PostExampleAsync";
private Activity mActivity;
private onServiceCallCompleted mListener;
private static final String URL = "http://54.169.88.65/events/eventmain/get_users.php";
public PostExampleAsync(Activity act)
{
this.mActivity = act;
this.mListener = this;
}
@Override
protected Boolean doInBackground(final String... params)
{
final String url = mMyPrefs.getString(URL, "");
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(mActivity);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Log.d(TAG, "Response from Service received");
// First validation
try
{
JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
// Call onServiceCallComplete
mListener.onServiceCallComplete(jsonObject);
}
catch (JsonParseException e)
{
// @TODO: Catch the case when we get back something other than a valid Json
Log.e(TAG, e.getMessage());
}
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
// @TODO: Catch error and print out proper message.
Log.e(TAG, "Something went wrong");
}
})
{
@Override
protected Map<String, String> getParams()
{
Map<String, String> postParams = new HashMap<>();
if (params.length > 0)
{
postParams.put("json", params[0]);
}
return postParams;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
return true;
}
@Override
public boolean onServiceCallComplete(JsonObject response)
{
String responseString = response.toString();
// @TODO: Do something with the results here
return true;
}
}
<强> onServiceCallCompleted.java 强>
package me.monori.examples.post.interfaces;
import com.google.gson.JsonObject;
/**
* Created by Antal Janos Monori on 10-05-2015.
* Copyright 2015 monori.me. All rights reserved.
*/
public interface onServiceCallCompleted {
/**
* A method call, that is triggered when a service call is finished, by a class that implements this interface.
* @param response The raw String response from the service
* @return boolean true if successful, false otherwise
*/
boolean onServiceCallComplete(JsonObject response);
}
这个例子假设如下: