从服务器打印数据有问题

时间:2016-06-27 22:31:46

标签: php android

应该回复“test”用户的密码 - > 123 但 它打印这个

http://8pic.ir/images/27z7hmtzxcooumdx0ccz.png 关于使用javascript和...的事情。

  • AndroidManifest ==> INTERNET premission

请帮帮我

MyCodes:

MainActivity

package com.example.ok;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

public static String res = "";

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

    Button btn = (Button) findViewById(R.id.button1);

    new login("http://sharjemoft.skyf.ir/test.php/","").execute();


    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Toast.makeText(getApplicationContext(),res, Toast.LENGTH_LONG).show();

        }
    });
}

}

登录

package com.example.ok;

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.channels.AsynchronousCloseException;

import org.apache.http.entity.InputStreamEntity;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

public class login extends AsyncTask{

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



public login(String link,String user){

    Link=link;
    User=user;

}



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


    try{

        String         data=URLEncoder.encode("username","UTF8")+"="+URLEncoder.encode(User,"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);

        }

        MainActivity.res=sb.toString();




    }catch(Exception e){



    }



    return "";
}

}

test.php的

<?php
$con=mysql_connect("sql305.skyf.ir","skyf5_18376000","milad137");
mysql_select_db("skyf5_18376000_test",$con);

$user=$_POST['username'];
$pass=$_POST['password'];

$sqlQ="select * from users where Username='$user' ";
$result=mysql_Query($sqlQ);
$row=mysql_fetch_array($result);

if($row[0]){
echo $row[1];
}else{
echo "no"; 
}
mysql_close($con);


?>

1 个答案:

答案 0 :(得分:0)

在client(android)-server。

之间进行交互时,您应该返回JSON响应

This帖子是如何做到这一点的一个很好的例子。总之,您应该做的是创建一个JSON对象,并将该对象返回给客户端:

echo(json_encode($your_response));