gpt4 book ai didi

android - HttpPost 请求响应

转载 作者:行者123 更新时间:2023-11-29 21:00:39 26 4
gpt4 key购买 nike

我有一个网页用 1 (true) 或 0 (false) 响应日志记录请求。当我尝试用另一个网页调用请愿书时,结果是正确的,它可以是 0 或 1。但是当我用 Android 应用程序调用它时,结果总是 0。

这是我的代码:

protected String doInBackground(String... params) {

InputStream inputStream = null;

String result = "";

String sJSon = "";

HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost("http://myURL");



//Build jsonObject

JSONObject jsonObject = new JSONObject();

try {

jsonObject.accumulate("token", "123456789");

} catch (JSONException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

jsonObject.accumulate("passw", "test");

} catch (JSONException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

jsonObject.accumulate("email", "test@test.com");

} catch (JSONException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

// Convert JSONObject to JSON to String

sJSon = jsonObject.toString();



//Encoding POST data

try {

httpPost.setEntity(new StringEntity(sJSon));

//Set some headers to inform server about the type of the content

httpPost.setHeader("Accept", "application/json");

httpPost.setHeader("Content-type", "application/json");

} catch (UnsupportedEncodingException e) {

// log exception

e.printStackTrace();

}



//making POST request.

try {

HttpResponse response = httpClient.execute(httpPost);

// write response to log



// receive response as inputStream

inputStream = response.getEntity().getContent();



// convert inputstream to string

if(inputStream != null)

result = convertInputStreamToString(inputStream); //THE RESULT SHOULD BE 1 AND NO 0 WHEN THE LOGGING IS OK. BUT IT IS ALWAYS 0

else

result = "Did not work!";

} catch (ClientProtocolException e) {

// Log exception

e.printStackTrace();

} catch (IOException e) {

// Log exception

e.printStackTrace();

}


if (result == "1"){
Intent intent = new Intent(LoginActivity.this, MenuActivity.class);

startActivity(intent);
}

return null;

}



private static String convertInputStreamToString(InputStream inputStream) throws IOException{

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

String line = "";

String result = "";

while((line = bufferedReader.readLine()) != null)

result += line;



inputStream.close();

return result;

}

谢谢!

最佳答案

如果请求没有返回1,则预计数据不正确。错误可以按值或按格式(不是 json)。

关于android - HttpPost 请求响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26230583/

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