我不能通过邮件将数据从android发送到服务器

时间:2014-10-15 17:25:28

标签: android eclipse

我无法将数据从andorid设备发送到网址 请帮我 ! 这是我的班级 我的代码有什么问题?

package com.example.test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.os.AsyncTask;



public class loginserver extends AsyncTask{

    private String Link="";
    private String User="";
    private String Pass="";



    public loginserver(String link,String user,String pass){

        Link=link;
        User=user;
        Pass=pass;
    }



    @Override
    protected String doInBackground(Object... arg0) {


        try{

            String data=URLEncoder.encode("username","UTF8")+"="+URLEncoder.encode(User,"UTF8");
            data+="&"+URLEncoder.encode("password","UTF8")+"="+URLEncoder.encode(Pass,"UTF8");



            URL mylink=new URL(Link);
            URLConnection connect=mylink.openConnection();

            connect.setDoOutput(true);
            OutputStreamWriter wr=new OutputStreamWriter(connect.getOutputStream());
            wr.write(data);
            wr.flush();


            BufferedReader reader=new BufferedReader(new InputStreamReader(connect.getInputStream()));
            StringBuilder sb=new StringBuilder();

            String line=null;

            while((line=reader.readLine()) !=null){

                sb.append(line);

            }

            Main.res = sb.toString();




        }catch(Exception e){



        }



        return "";
    }

}

这是我的主要代码 类代码或主代码中的问题?

package com.example.test;

import java.util.Timer;
import java.util.TimerTask;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebStorage.Origin;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Main extends Activity {

    public static String res="";
    private EditText usertext,passtext;
    private Button login,register,exit;


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

        usertext =(EditText) findViewById(R.id.usertext);
        passtext =(EditText) findViewById(R.id.passtext);


        login =(Button) findViewById(R.id.login);
        exit =(Button) findViewById(R.id.exit);
        register =(Button) findViewById(R.id.register);

        login.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

login(usertext.getText().toString(),passtext.getText().toString());


            }
        });


    }

 private void login (String user,String pass ){

     new loginserver("my address",user,pass ).execute();

     final ProgressDialog pd = new ProgressDialog(Main.this);
     pd.setMessage("please wait");
     pd.show();

     final Timer tm = new Timer();
     tm.scheduleAtFixedRate(new TimerTask(){
        public void run() {
        runOnUiThread(new Runnable() {
            public void run() {

                if(res.equals("")){
                    pd.cancel();
                    Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show(); 
                    tm.cancel();
                }

            }
        });

        }

     },1, 1000);
 }

}

php代码工作正常 - 但我无法从Android设备发送数据 是否有人知道我的错误?

1 个答案:

答案 0 :(得分:0)

首先,您必须在清单中设置互联网权限。

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

如果您首先检查网络是否可用,也会有所帮助。

private boolean networkIsAvailable(Context con) {
    ConnectivityManager cm = (ConnectivityManager) 
        con.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}

之后,您必须使用HttpClient。 查看这篇文章如何做到这一点:

Android HTTP-POST