将点击按钮上的手机号码发送到Android中的服务器URL

时间:2015-06-04 10:16:59

标签: java android button server

我想创建一个用户输入手机号码的应用,然后点击用户手机号码发送到服务器URL的继续按钮。这是我的代码

MainActivity.java

public class MainActivity extends Activity {

    private static final String TAG = "Http Connection";
    private EditText mobileno;
    private Button proceed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mobilenumber);

        mobileno = (EditText) findViewById(R.id.editText);
        proceed = (Button) findViewById(R.id.button);
        proceed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AsyncHttpTask asyncHttpTask = new AsyncHttpTask();
                final String url = "http://javatechig.com/api/get_category_posts/?dev=1&slug=android";
                String MobileNo = mobileno.getText().toString();
                new AsyncHttpTask().execute(url);
            }
        });

    }


    public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Attempting Proceed..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
            pDialog.dismiss();
        }
        @Override
        protected Integer doInBackground(String... params) {

            InputStream inputStream = null;

            HttpURLConnection urlConnection = null;

            Integer result = 0;
            try {
                /* forming th java.net.URL object */
                URL url = new URL(params[0]);
                String mobileNo = mobileno.getText().toString();
                urlConnection = (HttpURLConnection) url.openConnection();

                 /* optional request header */
                urlConnection.setRequestProperty("Content-Type", "application/json");

                /* optional request header */
                urlConnection.setRequestProperty("Accept", "application/json");

                /* for Get request */
                urlConnection.setRequestMethod("GET");

                int statusCode = urlConnection.getResponseCode();

                /* 200 represents HTTP OK */
                if (statusCode ==  200) {

                    inputStream = new BufferedInputStream(urlConnection.getInputStream());

                    String response = convertInputStreamToString(inputStream);

                    parseResult(response);

                    result = 1; // Successful

                }else{
                    result = 0; //"Failed to fetch data!";
                }

            } catch (Exception e) {
                Log.d(TAG, e.getLocalizedMessage());
            }

            return result; //"Failed to fetch data!";
        }


        @Override
        protected void onPostExecute(Integer result) {


            if(result == 1){

                Toast.makeText(MainActivity.this,result, Toast.LENGTH_LONG).show();
            }else{
                Log.e(TAG, "Failed to fetch data!");
            }
        }
    }


    private String convertInputStreamToString(InputStream inputStream) throws IOException {

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

        String line ;
        String result=null ;

        while((line = bufferedReader.readLine()) != null){
            result += line;
        }

            /* Close Stream */
        if(null!=inputStream){
            inputStream.close();
        }

        return result;
    }
    private void parseResult(String result) {

        try{
            JSONObject response = new JSONObject(result);

        }catch (JSONException e){
            e.printStackTrace();
        }
    }
}

这是我的xml文件:

mobilenumber.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/background"

    android:weightSum="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/boton"
            android:layout_width="110dp"
            android:layout_height="110dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginStart="0dp"
            android:layout_margin="110dp"
            android:background="@drawable/logo"
            android:gravity="center_horizontal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Your Number"
            android:textSize="40sp"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="4dp"
            android:textColor="#FFFFFF"
            android:textStyle="normal"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:inputType="number"
            android:ems="10"
            android:textSize="24sp"
            android:hint="        +91 Enter Number"

            android:digits="0123456789"
            android:textColorHighlight="@color/abc_primary_text_material_dark"
            android:textColorHint="#FFFFFF"
            android:textStyle="normal"
            android:background="#5fffffff"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="35dp"
            android:id="@+id/editText"
            android:layout_gravity="center_horizontal" />



    <Button
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:textSize="20dp"
            android:text="PROCEED"
            android:textStyle="bold"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="35dp"
            android:layout_marginBottom="50dp"
            android:textColor="@color/bright_foreground_inverse_material_light"
            android:background="@drawable/yellow"
            android:id="@+id/button"
            android:layout_gravity="center_horizontal" />




</LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在服务器调用时,您没有通过MobileNo。

要这样做,

String MobileNo = mobileno.getText().toString();

final String url = "http://javatechig.com/api/get_category_posts/?dev=1&slug=android&mobile_no=" + MobileNo ;

mobile_no替换为相应的参数名称。