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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题