我正在尝试编写一个应用程序来发布传入的电话号码并获得响应并通过使用 toast 到屏幕来显示结果。它适用于模拟器和手机中的妻子,但不适用于 3G。通话结束时,toast 显示结果。我已经将 APN 更改为 Internet,但它不起作用。有没有办法用3G做到这一点?
广播接收器
if(TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (!client.contactExists(context, incomingNumber)){
try {
String response=client.login5(incomingNumber,hash);
JSONObject jObject = new JSONObject(response);
resusername = jObject.getString("name");
reshash = jObject.getString("hash");
Toast a= Toast.makeText(context,"\n"+"Telefon Numarası:" + incomingNumber+"\n"+"Adına Kayıtlı Kişi:" + resusername+"\n", Toast.LENGTH_LONG);
a.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
a.show();
// msg += "My phone number:" + client.getMy10DigitPhoneNumber()+"\n";
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* if(reshash==hash && frame.konrolnum(incomingNumber)!=true) {
msg+="User name is: "+resusername;
frame.createContact(resusername, incomingNumber);}*/
} }
客户端
public String login5(String tlf,String hash) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);
HttpPost httppost = new HttpPost("http://www.whois118.com/broadcast.php");
nameValuePairs.add(new BasicNameValuePair("telefon",tlf));
nameValuePairs.add(new BasicNameValuePair("hash", hash));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.v("log","Result :"+result);
}
catch (Exception e)
{
Log.v("log", "Error converting result " + e.toString());
}
return result;
}