Android 在wireshark跟踪中获取基于行的文本数据应用程序/json而不是json对象
我试图将数据作为json对象发送到服务器,但失败了。 进行wireshark跟踪,发现它是以基于行的文本数据发送的。 无法找到问题,请附加部分代码Android 在wireshark跟踪中获取基于行的文本数据应用程序/json而不是json对象,android,json,Android,Json,我试图将数据作为json对象发送到服务器,但失败了。 进行wireshark跟踪,发现它是以基于行的文本数据发送的。 无法找到问题,请附加部分代码 public void post(View view) { if (contents2 != null) { new JSONtask().execute(contents2); } } public class JSONtask extends AsyncTask<String, String, Stri
public void post(View view) {
if (contents2 != null) {
new JSONtask().execute(contents2);
}
}
public class JSONtask extends AsyncTask<String, String, String> {
public String result;
@Override
protected String doInBackground(String... params) {
try {
// 1. URL
URL url = new URL("URL");
final String contents2 = params[0];
// 2. Open connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 3. Specify POST method
conn.setRequestMethod("POST");
// 4. Set the headers
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
// 5. Add JSON data into POST request body
//`5.1 Use Jackson object mapper to convert Contnet object into JSON
//ObjectMapper mapper = new ObjectMapper();
// 5.2 Get connection output stream
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
// 5.3 Copy Content "JSON" into
wr.writeBytes(contents2);
// 5.4 Send the request
wr.flush();
// 5.5 close
wr.close();
// 6. Get the response
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 7. Print result
System.out.println(response.toString());
result = response.toString();
} catch (Exception e) {
result =e.getMessage();
}
return result;
}
protected void onPostExecute(String page)
{
Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();
}
}
公共作废帖子(查看){
if(contents2!=null){
新建JSONtask().execute(contents2);
}
}
公共类JSONtask扩展了异步任务{
公共字符串结果;
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
//1.网址
URL=新URL(“URL”);
最终字符串contents2=params[0];
//2.开放式连接
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//3.指定POST方法
conn.setRequestMethod(“POST”);
//4.设置标题
conn.setRequestProperty(“内容类型”、“应用程序/json”);
连接设置输出(真);
//5.将JSON数据添加到POST请求正文中
//`5.1使用Jackson对象映射器将Contnet对象转换为JSON
//ObjectMapper mapper=新的ObjectMapper();
//5.2获取连接输出流
DataOutputStream wr=新的DataOutputStream(conn.getOutputStream());
//5.3将内容“JSON”复制到
wr.writeBytes(contents2);
//5.4发送请求
wr.flush();
//5.5结束
wr.close();
//6.得到回应
int responseCode=conn.getResponseCode();
System.out.println(“\n向URL发送'POST'请求:“+URL”);
System.out.println(“响应代码:“+responseCode”);
BufferedReader in=新的BufferedReader(
新的InputStreamReader(conn.getInputStream());
字符串输入线;
StringBuffer响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
in.close();
//7.打印结果
System.out.println(response.toString());
结果=response.toString();
}捕获(例外e){
结果=e.getMessage();
}
返回结果;
}
受保护的void onPostExecute(字符串页)
{
Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG.show();
}
}
任何关于可以做什么或我缺少什么的建议都会大有帮助。
提前感谢。@Mohit thanx感谢您抽出时间,我发现了问题所在。
问题是对象不匹配,发送的数据与服务器端的预期不匹配,一旦正确,问题就得到解决。@Mohit,添加了logcat,我希望它能有所帮助。仍然存在问题…:(您的错误显示一些wifi问题…您能发布发生哪一行错误吗