gpt4 book ai didi

java - 通过 HttpPost 发送到 .net 服务的 JSONObject 正在接收 null JSONObject

转载 作者:行者123 更新时间:2023-12-01 04:18:18 25 4
gpt4 key购买 nike

我需要使用一个 json Webservice 方法,该方法接受字符串化的 jsonObject 作为参数,

这就是我制作 JSONObject 的方式

btnRegister.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

jsonObjSend = new JSONObject();

try{
jsonObjSend.put("FirstName", firstname.getText().toString());
jsonObjSend.put("LastName", lastname.getText().toString());
jsonObjSend.put("EmaillId", emailid.getText().toString());
jsonObjSend.put("Password", password.getText().toString());
jsonObjSend.put("MobileNumber", mobilenumber.getText().toString());
// jsonObjSend.put("Login_RememberMe", "true");



// JSONObject header = new JSONObject();
// header.put("deviceType","Android"); // Device type
// header.put("deviceVersion","2.0"); // Device OS version
// header.put("language", "es-es"); // Language of the Android client
// jsonObjSend.put("header", header);


}catch (JSONException e) {
e.printStackTrace();
}



new sendjsonObect().execute(URL);
}
});

}

公共(public)类 sendjsonObect 扩展 AsyncTask {

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub

String jsonObjRecv = HttpClient.SendHttpPost(params[0],jsonObjSend);
Log.d("jsonObjSend",""+jsonObjSend);
Log.d("JsonObjRecv",""+jsonObjRecv);
return jsonObjRecv.toString();

}

@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(Registration.this, result,Toast.LENGTH_LONG ).show();
Log.d("Result", ""+result);
}

}

这是我发出请求调用的方法

public static String SendHttpPost(String URL, JSONObject jsonData) {

try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);

StringEntity se=null;
try {
se = new StringEntity(jsonData.toString(),HTTP.UTF_8);

} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
se.setContentType("application/json");
se.setContentEncoding("UTF-8");
// Set HTTP parameters
httpPostRequest.setEntity(se);

Log.d("Test", ""+se);
// httpPostRequest.setHeader("Accept", "application/json");
// httpPostRequest.setHeader("Content-type", "application/json");

long t = System.currentTimeMillis();
HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");




// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();

if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}

// convert content stream to a String
String resultString= convertStreamToString(instream);
instream.close();
resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

// Transform the String into a JSONObject
// JSONObject jsonObjRecv = new JSONObject(resultString);
//// Raw DEBUG output of our received JSON object:
// Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");

return resultString;
}

}
catch (Exception e)
{
// More about HTTP exception handling in another tutorial.
// For now we just print the stack trace.
e.printStackTrace();
}
return null;
}

服务端接收到的 JSONObject 显示为 null。

我犯了什么错误?

最佳答案

您是否尝试过 application/x-www-form-urlencoded 作为内容类型。

httpPost Request.addHeader("content-type", "application/x-www-form-urlencoded");

关于java - 通过 HttpPost 发送到 .net 服务的 JSONObject 正在接收 null JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19226307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com