通过POST发送登录详细信息时,服务器返回无效用户

时间:2015-09-25 19:17:51

标签: android json post httpurlconnection

您好我正在尝试在Android上运行图片上传,我试图上传图片并解析返回的一些json数据。当我试图传递数据时,我得到500错误或json说它是一个无效的用户。我无法访问服务器端代码,因为它在clinets端,他们不会释放给我或给我访问。我慢慢疯了,因为我所看到的一切都说这应该有用,因为我自己的理智是有什么我错过或做错了我在这里错误地传递登录信息或者什么?这里有一双新鲜的眼睛,非常感谢。

 class PostImage extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        Log.i("Api Handler", "Beginning Image upload.");

        String crlf = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";

        try {

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //Compress image into Jpeg format to make the file small enough.
            image.compress(Bitmap.CompressFormat.JPEG, 90, bos);
            byte[] data = bos.toByteArray();

            String charset = "UTF-8";

            //Open Http Connection
            URL url = new URL(Utilities.uploadImageApi);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.addRequestProperty(URLEncoder.encode("username", charset), URLEncoder.encode("username", charset));
            conn.addRequestProperty(URLEncoder.encode("password", charset), URLEncoder.encode("password", charset));
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "image/jpeg");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            //conn.setRequestProperty("Content-Disposition", "form-statsDataArray; name=name; filename=" + fileName +".jpg");

            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + crlf);
            dos.writeBytes("Content-Disposition; form-statsDataArray; name=name" + "; filename=" + fileName + ".jpeg");
            dos.writeBytes(crlf);
            dos.write(data);
            dos.writeBytes(crlf);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
            dos.flush();
            dos.close();

            if(conn.getResponseCode() == 200) {
                InputStream in = new BufferedInputStream(conn.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder sb = new StringBuilder();

                String line;

                try {

                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {

                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                Log.i("Api Handler", sb.toString());

            } else {
                Log.i("Api Handler", "Bad Response from Image Upload " + conn.getResponseCode() +
                        " Message: " + conn.getResponseMessage());
                //Log.e("Api Handler", conn.getErrorStream().toString());
                InputStream i = new BufferedInputStream(conn.getErrorStream());
                //Get Error specifics
                BufferedReader reader = new BufferedReader(new InputStreamReader(i));
                StringBuilder sb = new StringBuilder();

                String line;

                try {

                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {

                    try {
                        i.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                Log.i("Api Handler", sb.toString());


            }

        } catch (Exception e) {
            Log.i("Api Handler", "Upload Failed");
            Log.e("Api Handler", "" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

由于明显的原因,我已经将传入的登录详细信息更改为通用用户名和密码,这些信息将成为稍后在我使用测试帐户时传入的变量。

0 个答案:

没有答案