dsfasdfsda234234 2015-08-29 15:15
浏览 57
已采纳

从免费主机获取数据并删除包含该数据的HTML标签(Android)

I have an application that get a very simple data from my free host (I uploaded a php file in my host), and show that in a textview in my application. The data that my app get from host is a very simple (a text that my php file send to my app is like the result of this code : print "user exist"; but with that "user exist" my application shows a HTML tag with it, I think it is because of my Host, but how can I delete that HTML tag and just get the result of my php file to my android application. the image : http://8pic.ir/images/5i9y87e8xp9wocu1qaui.png I don't know from where (android cod or php in my host) the problem can resove than I copy both of them here :

my HTML code :

<?php
    $con = mysql_connect("mysql14.000webhost.com", "mohad", "229");
    mysql_select_db("mydb", $con);
    $user = $_POST['username'];
    $result = mysql_query("select Password from tbl_users where Username = '$user' ");

    $row = mysql_fetch_array($result);

    if($row[0]){print "user exist";}else print "no user";


    mysql_close($con);
?>

my android code (Main activity) :

public class Main extends Activity {

public static String netdata;
private Button btnGet;
private TextView txtShowdata;

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

    txtShowdata = (TextView) findViewById(R.id.txtShowdata);

    btnGet = (Button) findViewById(R.id.btnGet);
    btnGet.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            new Getdata().execute("http://mohad.webuda.com/test.php", "mohad");
            txtShowdata.setText(netdata);
        }
    }); 

}// end onCreat()

}// end Class

and here is myAsync class :

public class Getdata extends AsyncTask <Object, Object, Object>{

StringBuilder strBuilder;
@Override
protected Object doInBackground(Object... params) {

    try{

        URL url_mylink = new URL(params[0].toString());
        URLConnection url_connect = url_mylink.openConnection();



        // Send to host
        String sendingdata = URLEncoder.encode("username", "UTF8") +"="+ URLEncoder.encode(params[1].toString(), "UTF8");
        url_connect.setDoOutput(true);
        OutputStreamWriter osw = new OutputStreamWriter(url_connect.getOutputStream());
        osw.write(sendingdata);
        osw.flush();


        // Get from host
        InputStreamReader isr = new InputStreamReader(url_connect.getInputStream());
        BufferedReader bufR = new BufferedReader(isr);
        String temp_line = null;
        strBuilder = new StringBuilder();
        while((temp_line=bufR.readLine()) != null){
            strBuilder.append(temp_line);
        }


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

    return strBuilder;
}// end doInBackground()

@Override
protected void onPostExecute(Object result) {
    super.onPostExecute(result);
    Main.netdata = result.toString();

}// end onPostExecute()

}// end Class
  • 写回答

1条回答 默认 最新

  • dongqindu8110 2015-08-31 20:10
    关注

    I found my answer, I explain the resolve here for others may have the same problem as mine. I add a pipe mark '|' at end of my comment in php code and use subString in android code like code below :

    public class Main extends Activity {
    
    public static String netdata;
    private Button btnGet;
    private TextView txtShowdata;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        txtShowdata = (TextView) findViewById(R.id.txtShowdata);
    
        btnGet = (Button) findViewById(R.id.btnGet);
        btnGet.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                netdata = "";
                new Getdata().execute("http://mohad.webuda.com/test.php", "mohad");
    
                final ProgressDialog pd = new ProgressDialog(Main.this);
                pd.setTitle("Please wait..");
                pd.show();
    
                final Timer timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                if(netdata != ""){
                                    pd.cancel();
                                    tafkik(netdata);
                                    txtShowdata.setText(netdata);
                                    timer.cancel();
                                }
                            }
                        });
                    }
                }, 1, 1000);
            }// end onclick()
        }); 
    
    }// end onCreat()
    
    private void tafkik(String temp){
    
        for(int i=0;i<temp.length();i++){
            if(temp.charAt(i)=='|'){
                netdata = temp.substring(0, i);
                break;
            }
        }
    }
    
    }// end Class
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器