无法导入JSONObject包

时间:2019-07-04 12:13:02

标签: java android gson retrofit2

我正在尝试将手机中的图片上传到服务器上,并且我正在使用retrofit2,所以我遵循了一个教程,该教程演示了如何进行操作,尽管大多数人都对这项工作感到满意,几个错误修复了这些错误,同时查看了此处提出的问题,但是随后出现一个我无法解决的问题

错误显示:expected begin_array but was string at line 1 column 1 path $

并且我了解到我需要从String而不是A JSONObject切换返回值,但是不幸的是,由于我在android中非常缺乏经验,所以我无法解决问题,唯一想到的是我尝试过在界面中使用Gson我将向您展示,但很遗憾,JSONObject仅接受从org.json导入,而不接受com.google.gson

这是我尝试上传的主要功能

package com.example.tuteur;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.annotations.SerializedName;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ajouter_photo extends AppCompatActivity implements View.OnClickListener {
    ImageView upload_img;
    EditText img_text;
    EditText img_titre;
    Button img_upload;
    Button img_choose;
    Bitmap bitmap;


    private static final int IMG_RQUEST = 777;
    //private static final int result_load_img = 1;
    //private static final String server = "http://192.168.1.3/pfe/saveImg.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ajouter_photo);
        img_choose= findViewById(R.id.choose_button);
        upload_img = findViewById(R.id.photo_upload);
        upload_img.setEnabled(false);
        img_upload = findViewById(R.id.upload_button);
        img_text = findViewById(R.id.upload_text_corps);
        img_titre = findViewById(R.id.upload_text_title);
        img_choose.setOnClickListener(this);
        img_upload.setOnClickListener(this);



    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case (R.id.choose_button):
            selectImage();
                break;
            case (R.id.upload_button):
               uploadimg();
                break;

        }
    }
    private void uploadimg()
    {
        String img=imageToString();
        String Title = img_titre.getText().toString();
        String content= img_text.getText().toString();
        ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

        Call<ImageClass> call= apiInterface.uploadeImage(Title,content,img);
        call.enqueue(new Callback<ImageClass>() {
            @Override
            public void onResponse(Call<ImageClass> call, Response<ImageClass> response) {
                ImageClass imageClass = response.body();
                Toast.makeText(ajouter_photo.this,"Server response:"+imageClass.getResponse(),Toast.LENGTH_LONG).show();
                upload_img.setVisibility(View.GONE);
                img_titre.setVisibility(View.GONE);
                img_text.setVisibility(View.GONE);
                img_choose.setEnabled(true);
                img_upload.setEnabled(false);
                img_text.setText("");
                img_titre.setText("");

            }

            @Override
            public void onFailure(Call<ImageClass> call, Throwable t) {
                Toast.makeText(ajouter_photo.this,"error: "+t.getMessage(),Toast.LENGTH_LONG).show();

            }
        });
    }

    private void selectImage()
    {
        Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(galleryIntent, IMG_RQUEST);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMG_RQUEST  && resultCode == RESULT_OK && data != null)
        {
            Uri path = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),path);
                upload_img.setImageBitmap(bitmap);
                upload_img.setVisibility(View.VISIBLE);
                img_titre.setVisibility(View.VISIBLE);
                img_choose.setEnabled(false);
                upload_img.setEnabled(true);

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

            }
           //upload_img.setImageURI(path);

        }
    }
    private String imageToString()
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
        byte[] imgByte = byteArrayOutputStream.toByteArray();
        return Base64.encodeToString(imgByte,Base64.DEFAULT);
    }

}

这是api接口

package com.example.tuteur;


import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface ApiInterface {

    @FormUrlEncoded
    @POST("upload.php")
    Call<ImageClass> uploadeImage(@Field("title") String title,@Field("content") String content,@Field("image") String image);

}

这是api客户端

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

    private static final String BaseUrl="http://192.168.1.8/pfe/imageupload/";
    private static Retrofit retrofit;

    public static Retrofit getApiClient()
    {
        if(retrofit== null)
        {
            Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();
            retrofit = new Retrofit.Builder()
                    .baseUrl(BaseUrl)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }
}

0 个答案:

没有答案