HI 我創建了一個應用程序,我隱藏了編輯文本框和兩個按鈕,點擊了按鈕事件後,文本框將在運行時彈出,它工作得很好,但我需要打字文本要通過服務器,幫助我。所有想法都歡迎在android中發送/接收來自遠程服務器的文本數據
回答
如果我正確地理解了你,你在文本框中有文本,並且你想把這些數據發送到服務器。你可以看一下這些教程/片斷中看到有關您的數據發佈到Web服務器:
http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
或Secure HTTP Post in Android
嗨,這段代碼將您的數據發送到編輯文本編輯的服務器。對於textview,只需通過textview的名稱更改edittext的名稱即可。下面是代碼:從服務器
package com.example.asynchttppost;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText value;
private Button btn;
private ProgressBar pb;
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
tv =(TextView)findViewById(R.id.TextView1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(value.getText().toString().length()<1){
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(value.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(), "Code 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://10.0.2.2/chotu/index.php");
HttpPost httppost = new HttpPost("http://192.168.1.13/educlinic/Widget/AndroidApp");
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? – 2013-07-16 02:22:27
@RajuGujarati爲此檢出http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ – 2013-07-16 05:52:15
這是否意味着使用GCM服務器實施雲到設備消息傳遞是不必要的? – 2013-07-16 06:14:32
獲取文本
package com.example.sonasys.net;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.sonaprintersd.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
public class SingleContactActivity extends Activity {
private static final String TAG_CONTACTS = "Contacts";
private static final String TAG_POSTLINE = "PostLine";
private static final String TAG_Post_Img = "Post_Img";
private static final String TAG_Post_Img_O = "Post_Img_O";
private static String url;
TextView uid, pid;
JSONArray contacts = null;
private ProgressDialog pDialog;
String details;
// String imagepath = "http://test2.sonasys.net/Content/WallPost/b3.jpg";
String imagepath = "";
Bitmap bitmap;
ImageView image;
String imagepath2;
ArrayList<HashMap<String, String>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
url = "http://test2.sonasys.net/MobileApp/GetSinglePost?UserId="
+ uid.getText() + "&Post_ID=" + pid.getText();
contactList = new ArrayList<HashMap<String, String>>();
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(SingleContactActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
// pDialog.setTitle("Post Details");
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
JSONObject c = contacts.getJSONObject(0);
details = c.getString(TAG_POSTLINE);
imagepath = c.getString(TAG_Post_Img);
imagepath2 = c.getString(TAG_Post_Img_O);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**/
TextView Details = (TextView) findViewById(R.id.details);
// Details.setText(details);
Details.setText(android.text.Html.fromHtml(details));
}
}
添加新的類服務處理
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
*
* */
public String makeServiceCall(String url, int method,List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
- 1. Android,發送和接收來自服務器的信息
- 2. 向遠程數據庫服務器發送數據和從遠程數據庫服務器發送數據
- 3. 發送和接收從Android服務器的PHP服務器的數據
- 4. 使用AJAX發送和接收來自服務器的信息
- 5. 使用Cocoa發送和接收來自服務器的XML
- 6. 在C#中向服務器發送/接收數據#
- 7. Rails:將POST數據發送到本地和遠程服務器
- 8. 將JMX數據發送到遠程服務器(JMX轉發器?)
- 9. 從java中的sockjs服務器發送/接收數據?
- 10. MySql合併來自遠程數據庫服務器的數據
- 11. 發送和接收來自服務器不工作的響應android
- 12. Android:發送/接收數據
- 13. Android應用程序發送文件到遠程服務器
- 14. 安卓:發送文件到服務器:PHP接收服務器
- 15. 同時發送/接收來自服務器的信息,在Python中?
- 16. 發送從Android應用程序數據的本地服務器
- 17. 如何發送數據弗朗服務到接收機的Android
- 18. 使Github在接收更新時推送到遠程服務器
- 19. 發送來自Android的GET請求到本地Web服務器
- 20. 遠程通知在從Android上的服務器收到後立即發送
- 21. 如何將數據發送到PHP服務器並接收數據到Android
- 22. C#websocket服務器接收來自javascript的文本
- 23. 如何接收服務器發送的所有數據?
- 24. 將文件從表單直接發送到遠程服務器
- 25. Android套接字無法接收來自PHP服務器的數據
- 26. 服務器客戶端發送/接收簡單文本
- 27. 通過Android發送和接收數據到Web服務
- 28. 的iOS接收來自服務器
- 29. 來自android服務的多重發送
- 30. 發送來自Android的Thread類的數據服務類
多隆你好,非常感謝你 – ragav 2011-03-09 06:50:07
嗨,我沒有得到解決方案可以請給我一個解決方案。我希望文本從文本框傳遞到服務器 – ragav 2011-03-09 08:40:20
您需要詳細說明什麼不起作用。告訴我們你有什麼類型的服務器,或許有點更多關於你做了什麼嘗試 – 2011-03-09 15:37:43