来自服务器的Android客户端请求和响应

时间:2012-07-05 07:02:04

标签: android mysql web-services

我是android的初学者,并为用户输入登录详细信息的应用程序设计了登录GUI。在Java代码中获取登录详细信息(在字符串中)后,如何将其传递给服务器以检查有效组合?此外,当新用户输入注册详细信息时,如何将其传递给服务器并将其存储在DB(MySQL DB)中? Web服务的目的是什么?我应该使用Web服务还是servlet(这不是唯一的事情,稍后在我的应用程序中我需要向/从服务器发送联系人详细信息)?

我已经学习了SOAP webservices。请指导我如何在android中开发这个应用程序。

2 个答案:

答案 0 :(得分:0)

在这篇文章Adding body of call to POST using HttpURLConnection

中查看我的回答

这将帮助您连接到Web服务并将数据传递到该服务

答案 1 :(得分:-1)

(使用eclipse) 试试这个 我做了一项研究,发现了这个

public class AndroidtestingActivity extends ListActivity  {

    /** Called when the activity is first created. */
    @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    List<String> r = new ArrayList<String>();

    try{

    //http post
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("YOUR SITE");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    }
    catch(Exception e){
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
   }

    //Convert response to string  
    try
    {
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));

      sb = new StringBuilder();

      String line = null;

      while ((line = reader.readLine()) != null) 
      {
         sb.append(line + "\n");
      }

      is.close();

      result = sb.toString();
    }
    catch(Exception e)
    {
        Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
    }
    //END Convert response to string   
    try{
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data=null;
            for(int i=0;i<jArray.length();i++)
            {
               json_data = jArray.getJSONObject(i);                   
               r.add(json_data.getString("name")+json_data.getString("id"));
           }
           setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r));
        }
        catch(JSONException e1){
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
        } catch (ParseException e1) {
            Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
    }

}

public boolean onCreateOptionsMenu(android.view.Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);
    MenuInflater blowUp = getMenuInflater();
    blowUp.inflate(R.menu.cool_menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()){
    case R.id.iadd:
        Intent i = new Intent("android.intent.action.ADD");
        startActivity(i);

        break;  
    }
    return false;
}
}

将Android连接到服务器必须使用PHP Android - PHP - 服务器/数据库