duanqin9507
duanqin9507
2015-10-20 08:54
浏览 77

无法从服务器获取图像到Android应用程序

i am building application which have the ability to search users and get there information. the situation is that when i run it on the emulator everything is fine but when i run it on my real server sometimes i get the user detailed and sometimes not. i don't know why i get some users and other not. i check and the users that was does not exist i get the failed message but for other users that exists i get the message.

for example: correct result:

  {"flag":"user found","users":[{"name":"nir","email":"nirb@g.com","mobile":"0543536434","image":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH\/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH\/wAARCAB4AHgDASIAAhEBAxEB\/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL\/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6\/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL\/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6\/9oADAMBAAIRAxEAPwD+hr\/g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xVrYLEVoYCcIVKMYw9pFt058q5k6bcXzaJq6b3\/wA6vHjxn4yzLxKxHhLw9mkeHcPh80wOErZvSnUo4lyqTpzilWpyl7jVS004Xta3wtv26x\/bR+P2g6iH1zU5o7dCPMY3t6uNruG+\/wAcjPXpgZIzkfVGqft96Pe\/DS2hGvZ8SN54uV\/tBSxVowqclw\/3txw2RgjoRkwftmeEvgtovw81xvDej\/ZtYRLvyphdRPgiGcr8ohUkkqDjd14yRnP4h19zw1wnwT4i5Zhs6\/1b\/sR4PFxSo06VKi6\/JBtc6i53pPmdldO\/flufk3GniF4peC+d5hwu+NVxRHMcuTeJq4nEYhYVVKrX7uU\/Z8lePsbXs1ZvVt69n4+8RP4r8V6trkknmtey7y+7dnBfuSfXsecnkgAnjK39A8Oaj4ju0s7FGMjyJGp8t2G5iAOn4Z78gYJ6\/avw\/wD2Cfi34wt4b62Fr5EiRzKJLWfJjcv\/ALeAcEZ79R82Of1rMM\/4d4WwlKnmmZ4XL6NKlTpUo158t4U1OMVFJdor\/h73\/njJOD+M+Pcfiq2RZJj85xGIr1K+InhaTqWqVqk5SlJyknZyu27vzbdrfBdFfq5J\/wAE2\/icbR1SOz87y12k28+S\/wAwP8XTuehxjk5FfNvxJ\/Yq+Knw4gubzVVgaGBd7iK1nU7d0mMEs3OAc+oKnoGryMt8SeCc1r\/VsJxBgJ15SUadL2r5qjbSXKnFbu3lqrNvf6TOfBHxSyHBzx+YcH5tTwlOLnVr\/V1yUopNvnfPdWSbe6tbVt2PKv2fviU\/wx8e6f4h+0\/Z0tpIG3+aYx+7mD8tu4+7n2BbgjJr9q\/DP\/BQT4bjR4BqviFftv8Ay1\/0+3\/D7zk9+\/qRjqa\/ntv9PuNOna3uFKuuc5Vl6MVPBHtn15HrmqNcXF3hnw1xxiKOOzRVlWp04whWw8oRcqcXJxTbi7rV6dHZ7s9Pw38c+OfCnB4vKs

when it failed i get:

{"flag":"user not found"}

for other existing users:

 <html><head><meta http-equiv="refresh" content="0;http://biz.nf/errors/403.html" /></head><body></body></html>

i do not have any idea why its happend:

i just found that if i take off those 2 lines

$img_path = "images/".$row["image"];

$imgdata = base64_encode(file_get_contents($img_path));

and 
"image" => $imgdata

the result is fine. does my image decoding process correct? because in 1 user i get the image but in others not.

this is my java code:

  package com.example.matant.gpsportclient.Controllers;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.internal.widget.AdapterViewCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.matant.gpsportclient.AsyncResponse;
import com.example.matant.gpsportclient.R;
import com.example.matant.gpsportclient.Utilities.ImageConvertor;
import com.example.matant.gpsportclient.Utilities.InviteUsersArrayAdapter;
import com.example.matant.gpsportclient.Utilities.InviteUsersListRow;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import java.util.List;

public class InviteUsersActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener{
    private EditText editTextSearch;
    private Button btnSave,btnDiscard;
    private ListView usersListView;
    private List<InviteUsersListRow> rowUser;
    private DBcontroller dbController;
    public static final String EXTRA_USERS  = "";
    ListView listViewUsers;
    List<InviteUsersListRow> rowUsers;
    private InviteUsersArrayAdapter Useradapter = null;



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

        editTextSearch = (EditText) findViewById(R.id.editTextSearch);
        btnSave = (Button) findViewById(R.id.ButtonSave);
        btnDiscard = (Button)findViewById(R.id.ButtonDiscard);
        editTextSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(editTextSearch.getText().length() > 0) {
                    Log.d("send request","searching...");
                    sendDataToDBController();
                }
                else{
                    Log.d("do nothing","doing nothing");
                   if(Useradapter != null) {
                       Log.d("do nothing","clear adapter");
                       /*Useradapter.clear();
                       Useradapter.notifyDataSetChanged();*/
                       listViewUsers.setAdapter(null);
                   }
                }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        btnDiscard.setOnClickListener(this);
        listViewUsers = (ListView) findViewById(R.id.listViewusers);
        listViewUsers.setItemsCanFocus(true);
        btnSave.setOnClickListener(this);
    }

    @Override
    public void handleResponse(String resStr) {
        Log.d("invite_Response", resStr);
        if (resStr != null) {
            try {

                JSONObject json = new JSONObject(resStr);
                String flg = json.getString("flag");

                Log.d("flag",flg);
                switch (flg){

                    case "user found":{
                        JSONArray jsonarr = json.getJSONArray("users");
                        Log.d("array",jsonarr.toString());
                        rowUsers = new ArrayList<InviteUsersListRow>();
                        for(int i = 0; i < jsonarr.length();i++){
                            {
                                Log.d("user is", jsonarr.getJSONObject(i).toString());
                                String name = jsonarr.getJSONObject(i).getString("name");
                                String mobile = jsonarr.getJSONObject(i).getString("mobile");
                                Bitmap profileImage = ImageConvertor.decodeBase64(jsonarr.getJSONObject(i).getString("image"));
                                int  imgStatus = R.drawable.add_user_50;
                                if(Useradapter != null){
                                    Log.d("Useradapter invited size:",String.valueOf(Useradapter.getUsers().size()));
                                    for(int j = 0 ;j<Useradapter.getUsers().size();j++)
                                    {
                                        if(Useradapter.getUsers().get(j).getDesc().equals(mobile))
                                        {
                                             imgStatus = R.drawable.remove_user_50;
                                            Log.d("set status2.1",String.valueOf(imgStatus));
                                            break;
                                        }
                                    }
                                }else{

                                    imgStatus = R.drawable.add_user_50;
                                    Log.d("set status1",String.valueOf(imgStatus));
                                }
                                InviteUsersListRow rowUser = new InviteUsersListRow(imgStatus, name, mobile,profileImage);
                                rowUsers.add(rowUser);
                            }
                            if(Useradapter == null)
                                Useradapter = new InviteUsersArrayAdapter(this,R.layout.invite_users_listview_row,rowUsers);
                            else {
                                {
                                    Useradapter.setData(rowUsers);
                                    Useradapter.notifyDataSetChanged();
                                }
                            }
                            listViewUsers.setAdapter(Useradapter);

                        }
                        break;
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }else
             Log.d("ServiceHandler", "Couldn't get any data from the url");
    }

    @Override
    public void sendDataToDBController() {

        String username = editTextSearch.getText().toString();
        BasicNameValuePair tagreq = new BasicNameValuePair("tag", "search_user");
        BasicNameValuePair name = new BasicNameValuePair("name", username);
        List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
        nameValuePairList.add(tagreq);
        nameValuePairList.add(name);
        dbController = new DBcontroller(this,this);
        dbController.execute(nameValuePairList);

    }





    @Override
    public void preProcess() {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.ButtonDiscard:
            {
                Intent i = new Intent();
                setResult(RESULT_CANCELED,i);;
                finish();
                break;
            }

            case R.id.ButtonSave:
            {

                if(Useradapter != null )
                {
                    if(Useradapter.getUsers().size() > 0)
                    {
                        Log.d("adapter size:",String.valueOf(Useradapter.getUsers().size()));
                        Intent i = getIntent();


                        JSONArray jsonArr = new JSONArray();
                        for(int j = 0 ;j < Useradapter.getUsers().size();j++)
                        {

                            try {
                                JSONObject jsonObj = new JSONObject();
                                jsonObj.put("name",Useradapter.getUsers().get(j).getTitle());
                                jsonObj.put("mobile",Useradapter.getUsers().get(j).getDesc());
                                jsonArr.put(jsonObj);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                        Log.d("json users",jsonArr.toString());
                        i.putExtra("userList", jsonArr.toString());
                        setResult(RESULT_OK, i);
                        finish();
                    }
                    else{
                        Log.d("adapter size <0", "search is empty");
                        Toast.makeText(this, "Please select user!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            break;

        }

    }
}

this is the server side:

<?php
/**
 * Created by PhpStorm.
 * User: matant
 * Date: 9/24/2015
 * Time: 11:29 AM
 */
include 'response_process.php';
class SearchUser implements ResponseProcess{


    public function dataProcess($dblink)
    {
        $output = array();
        $name = $_POST["name"];
        $output["flag"]="user found";
        $query = "SELECT * FROM users WHERE users.name LIKE '$name%'";

        $result = mysqli_query($dblink,$query) or die (mysqli_error($dblink));

        if(!$result){
            $output["flag"] = "query failed";
            $output["query_msg"] = $result;

        }else{
            $no_of_rows = mysqli_num_rows($result);
            if ($no_of_rows < 1)
                $output["flag"]="user not found";   //user not found
            else{
                $output["flag"]="user found";
                $output["users"] = array();
                while($row = mysqli_fetch_assoc($result))
                {
                    $img_path = "images/".$row["image"];

                    $imgdata = base64_encode(file_get_contents($img_path));
                    $output["users"][] =  array("name"=> $row["name"],"email"=> $row["email"],"mobile" => $row["mobile"],"image" => $imgdata);
                }


            }



        }

        echo json_encode($output);

    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxianliu6756
    douxianliu6756 2015-10-20 11:43
    已采纳

    i found the problem. for some reason the size of the image was effect on the transferred so i re size the bitmap by 50%.

    now everything is working.

    点赞 评论

相关推荐