服务器端工作良好的客户端在Android中给0

时间:2015-02-11 06:24:55

标签: java android

在下面的代码System.out.println(groupdetails)中;给出0,但是parms打印出正确的值。

从服务器端它工作正常但从客户端给出0.我在哪里犯了错误?

的java

protected void onCreate(Bundle savedInstanceState) 
    {       
        super.onCreate(savedInstanceState);
          if (android.os.Build.VERSION.SDK_INT > 9) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }
        setContentView(R.layout.list_screen);

        friendAdapter = new FriendListAdapter(this);
        try {
            imService=Login.imService;
            String groupdetails = imService.DispalyGroupDetails(imService.getUsername());
            System.out.println(groupdetails);
        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();
        }    

    }

imService.DispalyGroupDetails

public String DispalyGroupDetails(String username) throws UnsupportedEncodingException  {


            String params = 
                        "username="+ URLEncoder.encode(this.username,"UTF-8") + 
                        "&password="+ URLEncoder.encode(this.password,"UTF-8") +
                        "&action="  + URLEncoder.encode("DispalyGroupFDetails","UTF-8")+
                        "&";

            Log.i("PARAMS", params);
            return socketOperator.sendHttpRequest(params);      


    }

socketOperator.sendHttpRequest

public SocketOperator(IAppManager appManager) { 
    }


    public String sendHttpRequest(String params)
    {       
        URL url;
        String result = new String();



        try 
        {
            url = new URL(AUTHENTICATION_SERVER_ADDRESS);
            HttpURLConnection connection;
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);

            PrintWriter out = new PrintWriter(connection.getOutputStream());

            out.println(params);
            out.close();

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                            connection.getInputStream()));
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                result = result.concat(inputLine);              
            }
            in.close();         
        } 
        catch (MalformedURLException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }           

        if (result.length() == 0) {
            result = HTTP_REQUEST_FAILED;
        }

        return result;


    }

1 个答案:

答案 0 :(得分:1)

可能imServicenull,因为您正在将绑定服务作为普通服务启动,因此imService未初始化。开始使用绑定服务bindService而不是startService

Intent intent=new Intent(Login.this,  IMService.class);
this.bindService(intent,mConnection,Context.BIND_AUTO_CREATE);