您好我是Android开发的新手,特别是从应用程序连接到服务器。我目前正试图将我的android应用程序连接到WAMP服务器上的数据库。我希望将来能够以此作为登录的手段,但同时我只想找回存储在数据库中的数据。我现在对这一切都感到困惑,并且看了很多教程,我的代码看起来与所有教程中发布的代码完全相同,并且我遵循了所有步骤。在Eclipse中使用WAMP服务器连接Android
我会非常感谢任何人可以帮助我,因为我真的坚持这一点,无法找到解决我的问题。
非常感谢。请在下面找到我的代码..
package eoghan.android;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class FoodPHPActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String result = null;
InputStream is = null;
StringBuilder sb=null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/wamp/www/Food/food.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Food_Name", "Apple"));
httppost.setEntity((HttpEntity) new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass 123 ", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parsing data
int fd_id = 0;
String fd_name = null;
try{
System.out.println("Entering the try method to connect the JSON");
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
fd_id=json_data.getInt("Food_ID");
fd_name=json_data.getString("Food_Name");
Toast.makeText(getBaseContext(), "JSON works", Toast.LENGTH_LONG).show();
}
}
catch(JSONException e1){
Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show();
}
catch (ParseException e1){
e1.printStackTrace();
}
System.out.println(fd_id);
System.out.println(fd_name);
}
}
我已经包含行:
uses-permission android:name="android.permission.INTERNET"
,并没有成功,随后其他几个工作的例子呢。
我真的很感谢任何帮助,这将有助于我。
您的问题究竟是什么? –
除非你想我们猜测你的确切错误,那么你在这里粘贴logcat错误呢? – Urban