2013-02-06 74 views
1

我想創建一個Android應用程序,可以發送數據到PHP服務器使用JSON。我遵循這個指南:http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/這裏是我的Android代碼:Android將不會發送數據到PHP使用JSON

import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class HelloWorldActivity extends Activity { 

    JSONParser jsonParser = new JSONParser(); 
    EditText userName; 

    // url to create new product 
    private static String url = "http://192.168.1.35/workspace/sosapp/db_connect.php";  

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Edit Text 
     userName = (EditText) findViewById(R.id.userName); 

     // Send button 
     Button sendButton = (Button) findViewById(R.id.sendButton); 

     // button click event 
     sendButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       String name = userName.getText().toString(); 

       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("name", name)); 

       // getting JSON Object 
       JSONObject json = jsonParser.makeHttpRequest(url, "POST", params); 

      } 
     }); 

    } 
} 

至於我的PHP代碼:

function insert($var1) { 

    // mysql inserting a new row 
    $result = mysql_query("INSERT INTO report (name) VALUES ('$var1')"); 

    // check if row inserted or not 
    if ($result) { 
     // successfully inserted into database; 
    $msg = "Message received."; 
    } else { 
    $msg = "Not received."; 
    } 
return ($msg); 
} 

if (isset($_POST['name'])) { 
    $name = $_POST['name']); 
    insert($name); 
} else { 
    echo "No input."; 
} 

我不斷收到無輸入。我運行Android應用程序(在我的手機連接到電腦),然後刷新PHP網站,但我一直沒有輸入。 Android應用程序不會發送數據。有人可以幫助嗎?非常感謝你!

+0

要JSON字符串發送到服務器只背景。作爲jsonObject或JsonArray? –

+0

請確保您能夠從瀏覽器訪問網絡服務... –

+0

是的,我可以從手機的瀏覽器訪問該地址。我想將它作爲一個數組發送。 – user1994644

回答

0

從我看到的,這是本教程的JSONParser類。它檢查

method == "POST" 

這是錯誤的。 將其更改爲

method.equals("POST") 

並查看它是否有效。

0

的Android HTTP是運行允許

import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class HelloWorldActivity extends Activity { 

    JSONParser jsonParser = new JSONParser(); 
    EditText userName; 

    // url to create new product 
    private static String url = "http://192.168.1.35/workspace/sosapp/db_connect.php";  

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Edit Text 
     userName = (EditText) findViewById(R.id.userName); 

     // Send button 
     Button sendButton = (Button) findViewById(R.id.sendButton); 

     // button click event 
     sendButton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       String name = userName.getText().toString(); 

       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("name", name)); 

       // getting JSON Object 

      Thread httpthread=new Thread(new Runnable() { 
      @Override 
      public void run() { 

      JSONObject json = jsonParser.makeHttpRequest(url, "POST", params); 


    runOnUiThread(new Runnable(){ 
      @Override 
      public void run() { 
       // ui working ex)textview.setText("test"); 


      } 
     }); 

      } 
     }); 

httpthread.start(); 
      } 
     }); 

    } 
} 
相關問題