gpt4 book ai didi

android - 来自 Android 设备的 Web 服务调用不起作用,在模拟器上运行良好

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:55 24 4
gpt4 key购买 nike

几个月前我开发了一些android应用程序项目,它包括,

  • GCM 功能
  • 网络服务调用

当时运行良好。昨天我尝试在真实设备上运行它,但我发现它无法进行任何网络服务调用。我原以为在模拟器上也是如此。但令我惊讶的是它在模拟器上运行良好......


我在真实设备上遇到的异常是:ConnectionTimeOutException


我想知道解决这个问题出了什么问题。我不确定我应该发布更多信息。请问你是否想让我发布一些东西

编辑:

AsyncTask<Void, Void, Boolean> gettingHttpOTP = new AsyncTask<Void, Void,Boolean>(){
HttpResponse httpresponse;
HttpClient client ;
JSONObject objSendjson;
HttpPost post ;
HttpEntity entity;
String result;
JSONObject objRetrievejson;

@Override
protected Boolean doInBackground(Void... arg0) {
client = new DefaultHttpClient();

HttpConnectionParams.setConnectionTimeout(client.getParams(), 50000);

objSendjson = new JSONObject();

try
{
post = new HttpPost(Configurations.API_VERIFY_END_USER);


objSendjson.put("Mobile_Number", gCountryCodeAndMobileNumber);
objSendjson.put("Signature_Key", Configurations.SIGNATUREKEY);
post.setHeader("Content-type", "application/json");
post.setEntity(new StringEntity(objSendjson.toString(), "UTF-8"));
httpresponse = client.execute(post);
entity = httpresponse.getEntity();
gHttpResponseCode=httpresponse .getStatusLine().getStatusCode();

if(gHttpResponseCode==200)
{
gResponseText = EntityUtils.toString(entity);

objRetrievejson=new JSONObject(gResponseText);

gRecievedJsonOutput=objRetrievejson.getString("Result_Output");
gRecievedJsonDescription=objRetrievejson.getString("Result_Message");
gRecievedJsonCode=objRetrievejson.getString("Result_Code");
gRecievedJsonStatus=objRetrievejson.getString("Result_Status");

}
else
{
gResponseText=null;
}
}
catch(Exception errorException)
{
Log.d("Exception generated with response code = "+gHttpResponseCode,""+
errorException);

}
return null;
}

我的网络服务在互联网上运行,我尝试使用不同的 ISP


编辑(2015 年 5 月 4 日):

我不知道是什么导致了这个问题,但我没有更改任何代码,但它又能正常工作了。

最佳答案

nobalG,试试我的代码

这是 JSONParser 类

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
Log.e("param--- is:-", "" + params);
// Making HTTP request
try {

// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

} else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
Log.e("-------------------------->", paramString.toString());
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);

HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

}

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

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");

}

Log.e("TAG", "sb.toString() >>>>>" + sb.toString());
json = sb.toString();
is.close();

} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}}

现在添加这个 Json 类引用。使用 post 方法调用网络服务并在我传递时传递 peram 并通过您的网络服务响应获取数据。

class GetUserDetail extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Please wait Insert Record ....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show()
}

protected String doInBackground(String... params) {

try {
// json class ref. from above class
JSONParser jsonpd = new JSONParser();
String url_login = "www.xyz.com/user_login.php"; // webservice url
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("email", elogin_email
.getText().toString()));
params1.add(new BasicNameValuePair("password", elogin_pass
.getText().toString()));
JSONObject json = jsonpd.makeHttpRequest(url_login, "POST",
params1); // webservice call and json responce in json
JSONObject mainJson = new JSONObject(json.toString());
cnt = GlobalArea.successresult(mainJson);
JSONArray json_contents = mainJson.getJSONArray("Success");
for (int i = 0; i < json_contents.length(); i++) {
JSONObject e = json_contents.getJSONObject(i);
EMAIL = e.getString("email");
CUSTOMER_ID = e.getString("customer_id");
PASSWORD = elogin_pass.getText().toString();
}
} catch (JSONException e) {

}
return null;
}

protected void onPostExecute(String file_url) {

try {
pDialog.dismiss();
if (cnt == 2) {
Toast.makeText(getApplicationContext(),
"Check Email Id and Password", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getApplicationContext(),
ToastStrings.LoginMessage, Toast.LENGTH_LONG).show();
}
} catch (Exception e) {

}
}
}

它对我来说很好用。用它。它可能对你有帮助。

关于android - 来自 Android 设备的 Web 服务调用不起作用,在模拟器上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29768845/

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