1

嗨,我有一个位置应用程序,我在 1/2 小时内将位置更新到服务器一次,它运行良好,但它使用 2.2 和 2.3 android 版本将值发布到服务器。在 3.0 及更高版本中,它正在获取 gps 坐标,而不是将值发布到服务器,有人能说出为什么会这样吗?但它运作良好,并在 2.2 和 2.3 上将值发布到服务器!

代码:

public class MainActivity extends Activity {

    String Message;
    DefaultHttpClient httpclient;
    HttpPost httppost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GetServerResponseRawString();
    }

    private String GetServerResponseRawString() {

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("imei", "400098890098"));
        nameValuePairs.add(new BasicNameValuePair("location", "13.116,80.208"));
        nameValuePairs.add(new BasicNameValuePair("latitude", "13.116"));
        nameValuePairs.add(new BasicNameValuePair("longitude", "80.208"));
    //  nameValuePairs.add(new BasicNameValuePair("logdate", "30-04-2013"));

        return GetServerResponseRawString(
                "http://197.168.1.1:90/LogGeo.aspx", nameValuePairs);

    }

    public String GetServerResponseRawString(String str_Url,
            List<NameValuePair> nvp_Parameter) {
        String str_Response = "";
        try {

            HttpResponse response;
            this.httpclient = new DefaultHttpClient();
            this.httppost = new HttpPost(str_Url);
            this.httppost.setEntity(new UrlEncodedFormEntity(nvp_Parameter,
                    HTTP.UTF_8));

            response = this.httpclient.execute(this.httppost);

            if (response != null
                    && response.getStatusLine().getStatusCode() == 200) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                response.getEntity().getContent(), "UTF-8"));
                StringBuilder builder = new StringBuilder();

                for (String line = null; (line = reader.readLine()) != null;) {
                    builder.append(line).append("\n");
                }
                Message = builder.toString().split("<")[0];
                str_Response = builder.toString();
                return str_Response;
            }
        } catch (Exception e) {
            String str_e = e.toString();
            return str_Response;

        }
        return str_Response;

    }
}

xml:

<uses-permission android:name="android.permission.INTERNET" />

谢谢..!

4

1 回答 1

0

您可以尝试通过 http 或 https 的异步发布方法

public class post extends AsyncTask<Void, Void, JSONArray> {
    @Override
    protected JSONArray doInBackground(Void... params) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata", "StringData"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            reader.close();
            String result11 = sb.toString();

            // parsing data
            return new JSONArray(result11);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(JSONArray result) {
        if (result != null) {
            // do something
        } else {
            // error occured
        }
    }
}
于 2014-02-12T08:50:04.170 回答