0

i want to post a variable to this file on webserver. i am using free hosting company. http://locationmeter.comule.com/reg.php it is doing good on emulator but it is not working on device. here is the code

public class MainActivity extends Activity {

    Button bSubmit;
    EditText name, age;

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

    private void init() {
        // TODO Auto-generated method stub
        name = (EditText) findViewById(R.id.editText1);
        bSubmit = (Button) findViewById(R.id.button1);
        bSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
                NameValuePair param = new BasicNameValuePair("name", name
                        .getText().toString());
                params.add(param);
                try {
                    HttpPost httpPost = new HttpPost(
                            "http://locationmeter.comule.com/reg.php");
                    httpPost.setEntity(new UrlEncodedFormEntity(params));

                    HttpClient client = new DefaultHttpClient();
                    HttpResponse response = client.execute(httpPost);
                    InputStream is = response.getEntity().getContent();
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(is));
                    String line = br.readLine();
                    do {
                        Toast.makeText(getBaseContext(), line, 300).show();
                        Log.d("", "usmann" + line);
                        line = br.readLine();
                    } while (line != null);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.postingvar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.postingvar.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

reg.php

     <?php
            if (isset($_POST['name'])) {
                $res = "success";
                echo json

_encode($res);

        }else{
            $res = "failed";
            echo json_encode($res);
        }
    ?>

from device i am getting a long html error page. please help

usmann<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
03-24 10:40:19.185: D/(413): usmann<html><head>
03-24 10:40:19.194: D/(413): usmann<meta http-equiv="Content-Type" CONTENT="text/html; charset=utf-8">
03-24 10:40:19.215: D/(413): usmann<title>ERROR: The requested URL could not be retrieved</title>
03-24 10:40:19.225: D/(413): usmann<style type="text/css"><!-- 
03-24 10:40:19.234: D/(413): usmann /*
03-24 10:40:19.246: D/(413): usmann Stylesheet for Squid Error pages
03-24 10:40:19.265: D/(413): usmann Adapted from design by Free CSS Templates
03-24 10:40:19.285: D/(413): usmann http://www.freecsstemplates.org
03-24 10:40:19.305: D/(413): usmann Released for free under a Creative Commons Attribution 2.5 License
03-24 10:40:19.315: D/(413): usmann*/
03-24 10:40:19.324: D/(413): usmann
03-24 10:40:19.335: D/(413): usmann/* Page basics */
03-24 10:40:19.355: D/(413): usmann* {
03-24 10:40:19.364: D/(413): usmann font-family: verdana, sans-serif;
03-24 10:40:19.375: D/(413): usmann}
03-24 10:40:19.395: D/(413): usmann
03-24 10:40:19.404: D/(413): usmannhtml body {
03-24 10:40:19.425: D/(413): usmann margin: 0;
03-24 10:40:19.435: D/(413): usmann padding: 0;
03-24 10:40:19.455: D/(413): usmann background: #efefef;
03-24 10:40:19.475: D/(413): usmann font-size: 12px;
03-24 10:40:19.495: D/(413): usmann color: #1e1e1e;
03-24 10:40:19.505: D/(413): usmann}
03-24 10:40:19.524: D/(413): usmann
03-24 10:40:19.555: D/(413): usmann/* Page displayed title area */
03-24 10:40:19.565: D/(413): usmann#titles {
03-24 10:40:19.595: D/(413): usmann margin-left: 15px;
03-24 10:40:19.614: D/(413): usmann padding: 10px;
03-24 10:40:19.636: D/(413): usmann padding-left: 100px;
03-24 10:40:19.654: D/(413): usmann background: url('http://www.squid-cache.org/Artwork/SN.png') no-repeat left;
03-24 10:40:19.665: D/(413): usmann}
03-24 10:40:19.686: D/(413): usmann
03-24 10:40:19.694: D/(413): usmann/* initial title */
03-24 10:40:19.716: D/(413): usmann#titles h1 {
03-24 10:40:19.725: D/(413): usmann color: #000000;
4

1 回答 1

0

我猜你的问题是关于“线程”。我想如果你在一个你的 Httppost 中AsyncTask你的问题将得到解决。如果我没有犯错,在 Android3 和更高版本中,你必须这样做。

于 2013-03-24T18:06:26.760 回答