2011-06-17 9 views
0

私は、ユーザーがウェブサイトで閲覧される曲情報を入力するアプリケーションを作成しています。私はこのHttpGetのリクエストを取得しようとしています。私は実際に情報を返すためにサーバを必要としません。私はMySQLデータベースに格納する情報が必要です。物事のPHP側で私は$ _GETを使って情報を引き出します。私はこれに間違った方法で近づいていますか?ここに私のアンドロイドコードがあります:Android HttpGet Webデータベースの挿入のリクエスト

public void executeHttpGet() throws Exception{ 
    BufferedReader in = null; 
    try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(
       "http://localhost:8888/?title=hello&artist=horray"); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader 
     (new InputStreamReader(response.getEntity().getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 
     String page = sb.toString(); 
     System.out.println(page); 
     } finally { 
     if (in != null) { 
      try { 
       in.close(); 
       } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

答えて

1

。 PHPを変更する必要がありますが、データベースにデータを挿入するので、GETを使用しないでください。これはまさにPOSTのためのものです。

また、 "localhost"はエミュレータで実行しているときに "localhost"が電話を意味するため、動作しません。エミュレータでは、「10.0.2.2」はコンピュータを意味します。だから私はそれがあなたが使いたいものだと仮定しています。

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost("10.0.2.2:8888"); 

try { 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("title", "hello")); 
    params.add(new BasicNameValuePair("artist", "horray")); 
    httpPost.setEntity(new UrlEncodedFormEntity(params)); 

    httpClient.execute(httpPost); 
    Log.i("posting", "Saved to server"); 
} catch (Exception e) { 
    Log.e("posting", e.getMessage()); 
} 

希望します。

関連する問題