gpt4 book ai didi

java - 如何在网络服务中发送电话号码并在android中接收请求

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:58 25 4
gpt4 key购买 nike

我在通过 http 客户端将数据发送到 post 方法时遇到麻烦。我想将任何电话号码发送到网络服务并接收请求。

这是我的 SignUp.java:-

public class SignUp extends AppCompatActivity implements View.OnClickListener {

TextView tv;
Button sign_up;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.sign_up);

tv = (TextView)findViewById(R.id.sign_up);
sign_up = (Button)findViewById(R.id.login_buttton);
et = (EditText)findViewById(R.id.sign_up_editText);

tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(SignUp.this, LoginActivity.class);
startActivity(intent);
}
});

sign_up.setOnClickListener(this);
}
public void onClick(View v){

validateForm();

/* if(et.getText().toString().isEmpty()){
Toast.makeText(SignUp.this,"Please Fill Contect Nuumber",Toast.LENGTH_LONG).show();
}
else{
postSignUpData();
}*/
}

public void postSignUpData(){
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();

}
private static boolean isValidPhoneNumber(String mobile) {
String regEx = "^[0-9]{10}$";
return mobile.matches(regEx);
}
private void validateForm(){
String mobile = et.getText().toString();

if(isValidPhoneNumber(mobile)){
requestForSMS();
}
else {
Toast.makeText(SignUp.this,"Please enter valid mobile number",Toast.LENGTH_LONG).show();
}
}

private void requestForSMS(){

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://opencart.codeniques.com/shopping/?route=feed/web_api/registertele&key=test123$");
try{
List<NameValuePair> nameValuePairs = new ArrayList<>(1);
nameValuePairs.add(new BasicNameValuePair("number",et.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
if(status==200){
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);

JSONObject jsonObject = new JSONObject(data);
Toast.makeText(SignUp.this,"Value of json object"+jsonObject,Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(SignUp.this,"GO to else",Toast.LENGTH_LONG).show();
}
//String str = inputStreamToString(response.getEntity().getContent()).toString();
// Toast.makeText(SignUp.this,"Str value "+str,Toast.LENGTH_LONG).show();
// HttpResponse response = httpclient.execute(httppost);
}catch (IOException |JSONException e){
Log.e("Error :",e.getMessage() );
}
}
private StringBuilder inputStreamToString(InputStream is){
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}

}

Here is a url for my web service

这是我的 Logcat:-

   E/AndroidRuntime:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
10-16 15:02:08.189 7560-7560/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
10-16 15:02:08.189 7560-7560/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
10-16 15:02:08.189 7560-7560/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at java.net.InetAddress.getAllByName(InetAddress.java:215)
10-16 15:02:08.189 7560-7560/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)

E/AndroidRuntime: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at shoppingmazza.android.catalyst.com.dynamiclayout.SignUp.requestForSMS(SignUp.java:102)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at shoppingmazza.android.catalyst.com.dynamiclayout.SignUp.validateForm(SignUp.java:86)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at shoppingmazza.android.catalyst.com.dynamiclayout.SignUp.onClick(SignUp.java:63)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at android.view.View.performClick(View.java:5198)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21147)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-16 14:24:44.827 7739-7739/shoppingmazza.android.catalyst.com.dynamiclayout E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)

我是提前android的新人。请帮忙。谢谢提前

最佳答案

这不是您的代码问题。您使用的是最低 api 级别 2.3 等。因此您需要在 asynctask 中执行网络操作或在代码之前使用以下代码以避免异常

if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}

关于java - 如何在网络服务中发送电话号码并在android中接收请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166658/

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