如何从Android应用程序发送5个字符串到网站(本地主机)

时间:2013-12-07 18:40:50

标签: android json localhost

我从少数网站获得了帮助,并开发了一个代码,用于将数据从应用程序发送到网站。但是这段代码给出了错误。帮我把申请表中的5个发送到网站。

公共类MainActivityServer扩展Activity {     私人ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
Button send1;
TextView name;
TextView number;
TextView email;
TextView suggestion;

 // url to create new product
private static String url_create_product = "http://api.androidhive.info/android_connect/create_product.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity_server);
    send1 = (Button)findViewById(R.id.Send);
    name= (EditText)findViewById(R.id.T1);
    number= (EditText)findViewById(R.id.T2);
    email= (EditText)findViewById(R.id.T3);
    suggestion= (EditText)findViewById(R.id.T4);

    send1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new CreateNewProduct().execute();
        }
    });
}   
public class CreateNewProduct extends AsyncTask<String, String, String>{
    protected String doInBackground(String... args) {

         String name = name.getText().toString();
         String number = number.getText().toString();
         String email = email.getText().toString();
         String suggestion = suggestion.getText().toString();
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new BasicNameValuePair("name", name));
         params.add(new BasicNameValuePair("price", number));
         params.add(new BasicNameValuePair("description", email));
         params.add(new BasicNameValuePair("description", suggestion));

         // getting JSON Object
         // Note that create product url accepts POST method
         JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                 "POST", params);

         // check log cat fro response
         Log.d("Create Response", json.toString());

         // check for success tag
         try {
             int success = json.getInt(TAG_SUCCESS);

             if (success == 1) {
                 // successfully created product
                 Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
                 startActivity(i);

                 // closing this screen
                 finish();
             } else {
                 // failed to create product
             }
         } catch (JSONException e) {
             e.printStackTrace();
         }

         return null;
     }

     /**
      * After completing background task Dismiss the progress dialog
      * **/




    //end postData()
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_activity_server, menu);
    return true;
}

}}

enter image description here

1 个答案:

答案 0 :(得分:0)

我确定这里有错误:

String url_create_product = "http://api.androidhive.info/android_connect/create_product.php";

这是androidhive.com服务器的网址。

您需要通过localhost或您自己的服务器替换它们。

确保您已修改manifest.xml文件,您需要设置Internet权限,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidhive"
    android:versionCode="1"
    android:versionName="1.0" >
    <application
        android:configChanges="keyboardHidden|orientation" 
        android:icon="@drawable/logo4"
        android:label="@string/app_name_phone" >
....
....
....
    </application>
....
....
....
....
....
....
    <!--  Internet Permissions -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>