MYSQL到android数据传输

时间:2011-07-10 06:21:16

标签: java php android mysql

我想将mysql数据传输到android手机,反之亦然。

到目前为止,我得到了.php文件以显示我需要的信息,但我无法获取一个代码,该代码将获取页面的代码并将其转换为android中的字符串或整数。

我目前正在使用此代码。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class version extends ListActivity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity entity = response.getEntity();
     is = entity.getContent();
     }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());
        }
//paring data
String version;
try{
      jArray = new JSONArray(result);
      JSONObject json_data=null;
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             version=json_data.getString("versioncode");
         }
      }
      catch(JSONException e1){
          Toast.makeText(getBaseContext(), "Error in getting version code. Please report." ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
            e1.printStackTrace();
    }
}
}

如何获取它以便将当前应用程序版本与我在mysql服务器中提供的字符串进行比较?

2 个答案:

答案 0 :(得分:0)

我尝试了你的代码,它对我有用。 使用“活动”代替“ListActivity”。

并确保您已在manifest.xml文件中授予“android.permission.INTERNET”权限。

答案 1 :(得分:0)

看起来你很近,我会这样做:

if (version.equals("1.0.2") {
  // same version
else {
  // different version
}