HTTP错误414.请求的URL太长,同时通过java中的数据传递到服务器android
我发送一个服务器请求与Android中的javacode,但我得到的响应代码为414.我是新来的android。请帮忙。我使用的是后method.I我用下面的代码:HTTP错误414.请求的URL太长,同时通过java中的数据传递到服务器android
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;
}
}
我的URL请求是这样的:
jsonObject=JSONfunctions.getJSONfromURL("http://54.169.88.65/events/eventmain/get_users.php?json="+URLencoded);
URLencoded
被转换成字符串JSON数据,然后进行了urlencoded。
首先,正如任何4xx错误代码所暗示的,它在客户端,而不是服务器端需要做些什么。
当你得到414,意味着你打的URL太长。
414 - 请求的URL太长,Web服务器与此错误 当它拒绝服务请求,因为请求的URL是 长于服务器愿意或能够解释响应。这种罕见的 条件只有当客户端不正确地将POST请求转换为具有长查询信息的GET请求时, 当客户端已经下降到重定向的URL“黑洞” (例如,重定向的URL前缀指向自己的后缀),或者当服务器受到试图利用固定长度缓冲区的某些服务器中存在的安全漏洞攻击时,客户端正在读取或操作请求URL时受到攻击。通常,对于真正的URL,Web服务器设置了 相当大的限制。最多2048或 4096个字符。如果长URL有效并且您收到414错误,则 可能需要重新配置Web服务器以允许此类URL通过 。
参考:here
这表明我,该字符串要附加到的结局是太长了,你可能不应该通过它的URL“JSON =?”。我也可以看到,你正在尝试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);
}
注
这个例子假定的弗洛wing:
- 你想发送POST请求而不是GET请求;如果是其他情况,请查看Volley documentation关于如何改变这种情况。
- 您期望从服务(响应不只是发送一个JSON字符串
- 要在异步任务运行。
- 您下载并执行截击库。
非常感谢 – Vicky
在服务器端-at web.config文件,增加大小maxReceivedMessageSize =“2147483647” maxBufferSize =“2147483647” maxBufferPoolSize =“2147483647” – Mahalakshmi