Java 如何在网络服务中发送电话号码并在android中接收请求
我在通过http客户端向post方法发送数据时遇到问题。我想向web服务发送任何电话号码并接收请求 这里是我的SignUp.java:-Java 如何在网络服务中发送电话号码并在android中接收请求,java,android,androidhttpclient,Java,Android,Androidhttpclient,我在通过http客户端向post方法发送数据时遇到问题。我想向web服务发送任何电话号码并接收请求 这里是我的SignUp.java:- public class SignUp extends AppCompatActivity implements View.OnClickListener { TextView tv; Button sign_up; EditText et; @Override protected void onCreate(Bundle savedInstanceStat
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;
}
我是新的advance android。请帮助。谢谢advance。您的代码没有问题。您正在使用min api level 2.3等。因此,您需要在asynctask中执行网络操作,或者在代码之前使用以下代码以避免异常
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
您的代码没有问题。您使用的是min api level 2.3等,因此您需要在asynctask中执行网络操作,或者在代码之前使用以下代码以避免异常
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
您面临的任何错误?我发布我的错误查看我编辑的问题在您的logcat帖子中也会提到一些异常,在第一行上方,查看我编辑的问题这是因为您在主线程中执行
NetworkCall
,使用AsyncTask
或Handler
将数据发布到您的Web服务您面临的任何错误?我发布我的错误查看我的编辑问题在您的日志猫帖子中也会提到一些异常,在第一行上方,查看我编辑的问题这是因为您在主线程中执行NetworkCall
,使用AsyncTask
或Handler
将数据发布到Web服务