我想通過HTTP發佈從android發送數據到web。但我不知道如何從android到web接收數據。安卓在網絡上發佈數據
這裏是我的活動代碼:
public class Chatit extends Activity {
Context context = this;
ChatDbHelper chatDbHelper;
SQLiteDatabase sqLiteDatabase;
EditText editText;
ListView listView;
Cursor cursor;
ListDataAdapter listDataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chatit);
editText = (EditText)findViewById(R.id.editText3);
listView = (ListView)findViewById(R.id.listView);
listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
listView.setAdapter(listDataAdapter);
chatDbHelper = new ChatDbHelper(context);
sqLiteDatabase = chatDbHelper.getReadableDatabase();
cursor = chatDbHelper.getChat(sqLiteDatabase);
if (cursor.moveToFirst()) {
do {
String name;
name = cursor.getString(0);
DataProvider dataProvider = new DataProvider(name);
listDataAdapter.add(dataProvider);
} while (cursor.moveToNext());
}
}
/* public void send(View view){
String chat = editText.getText().toString();
chatDbHelper = new ChatDbHelper(context);
sqLiteDatabase = chatDbHelper.getWritableDatabase();
chatDbHelper.addChat(chat,sqLiteDatabase);
Toast.makeText(getBaseContext(), "Message sent", Toast.LENGTH_SHORT).show();
chatDbHelper.close();
Intent intent = new Intent(this,Chatit.class);
startActivity(intent);
} */
public void send(View view) {
// TODO Auto-generated method stub
if(editText.getText().toString().length()<1){
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
String chat = editText.getText().toString();
chatDbHelper = new ChatDbHelper(context);
sqLiteDatabase = chatDbHelper.getWritableDatabase();
chatDbHelper.addChat(chat,sqLiteDatabase);
Toast.makeText(getBaseContext(), "Message sent", Toast.LENGTH_SHORT).show();
chatDbHelper.close();
Intent intent = new Intent(this,Chatit.class);
startActivity(intent);
// pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(editText.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double> {
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}
protected void onPostExecute(Double result){
// pb.setVisibility(View.GONE);
// Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}
// protected void onProgressUpdate(Integer... progress){
// pb.setProgress(progress[0]);
// }
public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://somewebsite.com/receiver.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}}}
請告訴我:
我做的正確與否?
我怎樣才能從android到web接收數據?