Из галереи устройства загружается изображение, переводится в формат Base64 и запросом отправляется на сервер. Сервер выдаёт:
Exception happened during processing of request from ('37.20.80.233', 1992)
Traceback (most recent call last):
File "/usr/lib64/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib64/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib64/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib64/python2.7/SocketServer.py", line 655, in __init__
self.handle()
File "/usr/lib64/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/usr/lib64/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
method()
File "serv2.0.py", line 44, in do_POST
data = json.loads(self.data_string)
File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 1 column 125 (char 124)
Через "Инсомнию" всё работает правильно, из чего можно сделать вывод ,что проблема на стороне клиента.
Ниже приведён код активити. При нажатии первой кнопки(метод onClick) открывается галерея, необходимо выбрать изображение ,которое переведётся в Base64. После чего, при нажатии кнопки 2 (метод onClick1) происходит отправка запроса на сервер.
public class avatar extends AppCompatActivity {
String nick;
byte errsit_l = 0;
byte non_conl = 0;
class AvatarResponser{
String status;
String image;
public String toStringAvatar(){
return "AvatarResponse{" + "status: " + status + '\'' + ",image:" + image + ')';
}
}
class AvatarRequest extends AsyncTask {
@Override
protected void onPreExecute() {
}
@Override
protected Object doInBackground(Object[] objects) {
if (encoderd_picture != null) {
String set_server_url = "http://194.176.114.21:8010";
URL url1 = null;
try {
url1 = new URL(set_server_url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (url1 != null) {
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url1.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
urlConnection.setDoOutput(true);
OutputStream out = null;
try {
out = urlConnection.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
String action = "avatar";
char c = '"';
String pin = "123";
nick = "painis";
String data = "{\"action\":" + c + action + c + "," + "\"nickname\":" + c + nick + c + "," + "\"image\":" + c + encoderd_picture + c + "," + "\"pin\":" + c + pin + c + "}";
Log.d("DATA",data);
try {
if (out != null) {
out.write(data.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
Gson gson = new Gson();
AvatarResponser response = null;
try {
response = gson.fromJson(new InputStreamReader(urlConnection.getInputStream()), avatar.AvatarResponser.class);
if(response!=null) {
response.toStringAvatar();
}
} catch (IOException e) {
e.printStackTrace();
}
if (response != null) {
response.toStringAvatar();
System.out.println(response.toStringAvatar());
urlConnection.disconnect();
if (response.status != "-1") {
Intent intent = new Intent(avatar.this, FriendList.class);
startActivity(intent);
} else {
errsit_l = 1;
}
return response;
} else {
non_conl = 1;
return 0;
}
}
return 0;
}
return 0;
}}
public void onClick1(View view){
if(encoderd_picture!=null){
AvatarRequest ara = new AvatarRequest();
ara.execute();
}
}
String encoderd_picture;
Button butt;
Bitmap bitmap = null;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent(avatar.this, avatar.class);
startActivity(intent);
}
return super.onKeyDown(keyCode, event);
}
static final int GALLERY_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// Intent intent = getIntent();
// nick = intent.getStringExtra("NAME");
// if (nick != null) {
// Log.d("NICKNAME",nick);
// }
butt = findViewById(R.id.button5);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_avatar);
if(butt !=null){butt.setEnabled(false);}
Button button = findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
}
});
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
ImageView imageView = findViewById(R.id.imageView);
switch(requestCode) {
case GALLERY_REQUEST:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);
byte[] bb = bos.toByteArray();
encoderd_picture = (String) Base64.encodeToString(bb,5);
Logger log = Logger.getLogger(avatar.class.getName());
log.info("ENCODED:"+encoderd_picture);
if(butt!=null){
butt.setEnabled(true);}
}
}
}
}
'''
Если судить по логам, то response=null. А вот почему он равен null я понять и не могу.