我有一个应用程序,它将一些数据提交到本地服务器,因此服务器将发回一个JSON。说{status:“success”}
当我在2.3 SDK模拟器中运行应用程序时,它工作,但安装在Galaxy Tab(2.2 SDK)中,相同的响应是以html的形式。
I/RESPONSE( 8190): <?xml version="1.0" encoding="utf-8"?>
I/RESPONSE( 8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
I/RESPONSE( 8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
I/RESPONSE( 8190): <html>
I/RESPONSE( 8190): <head>
I/RESPONSE( 8190): <title>417 Expectation Failed</title>
I/RESPONSE( 8190): </head>
I/RESPONSE( 8190): <body>
I/RESPONSE( 8190): <h1>Error 417 Expectation Failed</h1>
I/RESPONSE( 8190): <p>Expectation Failed</p>
I/RESPONSE( 8190): <h3>Guru Meditation:</h3>
I/RESPONSE( 8190): <p>XID: 1902486816</p>
I/RESPONSE( 8190): <hr>
I/RESPONSE( 8190): <address>
I/RESPONSE( 8190): <a href="http://www.varnish-cache.org/">Varnish cache server</a>
I/RESPONSE( 8190): </address>
I/RESPONSE( 8190): </body>
I/RESPONSE( 8190): </html>
I/RESPONSE( 8190): <--
I/RESPONSE( 8190):
I/RESPONSE( 8190): <?xml version="1.0" encoding="utf-8"?>
I/RESPONSE( 8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
I/RESPONSE( 8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
I/RESPONSE( 8190): <html>
I/RESPONSE( 8190): <head>
I/RESPONSE( 8190): <title>417 Expectation Failed</title>
I/RESPONSE( 8190): </head>
I/RESPONSE( 8190): <body>
I/RESPONSE( 8190): <h1>Error 417 Expectation Failed</h1>
I/RESPONSE( 8190): <p>Expectation Failed</p>
I/RESPONSE( 8190): <h3>Guru Meditation:</h3>
I/RESPONSE( 8190): <p>XID: 1902486816</p>
I/RESPONSE( 8190): <hr>
I/RESPONSE( 8190): <address>
I/RESPONSE( 8190): <a href="http://www.varnish-cache.org/">Varnish cache server</a>
I/RESPONSE( 8190): </address>
I/RESPONSE( 8190): </body>
I/RESPONSE( 8190): </html>
W/System.err( 8190): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of
W/System.err( 8190): <?xml version="1.0" encoding="utf-8"?>
W/System.err( 8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
W/System.err( 8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
W/System.err( 8190): <html>
W/System.err( 8190): <head>
W/System.err( 8190): <title>417 Expectation Failed</title>
W/System.err( 8190): </head>
W/System.err( 8190): <body>
W/System.err( 8190): <h1>Error 417 Expectation Failed</h1>
W/System.err( 8190): <p>Expectation Failed</p>
W/System.err( 8190): <h3>Guru Meditation:</h3>
W/System.err( 8190): <p>XID: 1902486816</p>
W/System.err( 8190): <hr>
W/System.err( 8190): <address>
W/System.err( 8190): <a href="http://www.varnish-cache.org/">Varnish cache server</a>
W/System.err( 8190): </address>
W/System.err( 8190): </body>
W/System.err( 8190): </html>
修改 请求发送: -
try {
HttpClient client = new DefaultHttpClient();
String postURL = GlobalCodes.getBaseurl();
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("show",
"testpost"));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
String str2 = EntityUtils.toString(resEntity);
Log.i("RESPONSE", " <--");
if (resEntity != null) {
Log.i("RESPONSE","**");
JSONObject jsonObj = new JSONObject(str2);
if (jsonObj.getString("status").equalsIgnoreCase("succuss")) {
.....
} else {
......
}
}
} catch (Exception e) {
e.printStackTrace();
}
任何人都可以告诉我哪里出错了?
快乐编码..!
答案 0 :(得分:4)
Galaxy Tab必须向模拟器发送不同的标头。尝试禁用HttpPost对象上的期望标题。
httppost.getParams().setBooleanParameter( "http.protocol.expect-continue", false )
答案 1 :(得分:3)
请检查此行:
W/System.err( 8190): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of
我确信你在服务器端创建/编码JSON对象时犯了一个错误。
答案 2 :(得分:3)
您的Java代码没有任何问题。问题出在服务器端。它也曾经发生在我身上,但是我的服务器团队改变了服务器并解决了问题。
简而言之, HTTP错误417 表示应用程序数据传输出现问题,因此服务器未收到预期的所有数据包(或者它们出现故障)。此错误仅由使用CheckUpDown的网站引发。这是CheckUpDown服务器的问题,因此您无能为力。您可能希望通过公共论坛通知 wallbase.cc 的问题管理员。
答案 3 :(得分:2)
看起来你的请求的接受标题是text / html而不是application / json。改变它。
答案 4 :(得分:1)
你得到error from server
这就是为什么这类数据会得到回应。
尝试handle error at server side
,查看从您的应用程序发送请求时获取的数据服务器类型,以及服务器如何处理它。
编辑:在我的情况下,这有效,
JSONObject jsonObject = new JSONObject();
jsonObject.put("key1", value1);
jsonObject.put("key2", value2);
JSONArray jArrayParam = new JSONArray();
jArrayParam.put(jsonObject);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("bulkdata",jArrayParam.toString()));
Log.e("bulkdata", jArrayParam.toString());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("yor remote server url");
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// get response entity
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream is = entity.getContent();
// convert stream to string
result = convertStreamToString(is);
result = result.replace("\n", "");
}
这是方法 convertStreamToString(is);
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
return sb.toString();
}
答案 5 :(得分:1)
也许这是DefaultHttpClient使用的默认值(用户代理字符串,严格传输编码等)的问题?尝试使用AndroidHttpClient
代替DefaultHttpClient
。其默认值可能更兼容http://developer.android.com/reference/android/net/http/AndroidHttpClient.html
如果这不起作用,您可能需要比较正在运行的iPhone,仿真器客户端发送的HTTP标头与损坏的Galaxy客户端的HTTP标头,并相应地更新设置。
答案 6 :(得分:1)
那是因为服务器回复了错误页面的html - 不完全是json-parseable。
答案 7 :(得分:1)
好吧rahul你有没有尝试过HTTP Post协议的任何不同类型的响应处理
我使用这样的东西。try {
String params;
String encodedDataLength;
connectURL = new URL(_uRL);
params = "Blah... Blah... blah;
encodedDataLength = "" + params.length();
conn = (HttpURLConnection) connectURL.openConnection();
//conn.setDoInput(true);
conn.setDoOutput(true); // signify a post communcation protocol
conn.setRequestProperty("User-Agent","mobile");
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "Content-Length", encodedDataLength);
//System.out.println("con :" + conn);
os = conn.getOutputStream();
os.write(params.getBytes());
os.flush();
// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
is = conn.getInputStream();
// get session from the cookie
String cookie = conn.getHeaderField("cookie");
if (cookie != null) {
session = cookie;
}
// Get the length and process the data
BufferedReader br = new BufferedReader(new InputStreamReader(is));
responseString = br.readLine();
//System.out.println(responseString);
} catch (Exception e) {
//java.lang.System.out.println("http exception: " + e.printStackTrace());
} finally {
try {
if (is != null)
is.close();
if (os != null)
os.close();
if (conn != null)
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
完成此操作后...您可以插入任何Json解码方法来从名为resposeString的字符串中检索数据。
以下是此实现的包含导入文件。我知道那些不同。
import java.io.*;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.zip.GZIPInputStream;
import java.lang.String;