0

I am using an Image generator api(reverse proxy) in my app but it is not working in mobile but it is working in android studio emulator (pixel 6 pro api 32).

When I click on generate image nothing happens and its progress bar just works and won't stop.

imageButton.setOnClickListener((v) -> {
            String text = editText.getText().toString().trim();
            if (text.isEmpty()) {
                editText.setError("Text can't be empty");
                return;
            }
            try {
                callapi(text);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
private void callapi(String text) throws IOException {
        JSONObject jasonbody = new JSONObject();

        try {
            jasonbody.put("prompt", text);
            jasonbody.put("size", "1024x1024");
        } catch (Exception e) {
            e.printStackTrace();
        }

        RequestBody sBody = RequestBody.create(jasonbody.toString(), JSON);

        Request request = new Request.Builder()
                .url("https://api.pawan.krd/v1/images/generations%22)
                .post(sBody)
                .header("Authorization", "Bearer ...)
                .header("Content-Type", "application/json")
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Toast.makeText(getApplicationContext(), "Failed to generate", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                try {
                    JSONObject json = new JSONObject(response.body().string());
                    imgurl = json.getJSONArray("data").getJSONObject(0).getString("url");
                    activity();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
ImageView imageView = findViewById(R.id.imageView2);

        runOnUiThread(()->{
            Picasso.get().load(MainActivity.imgurl).resize(1024,1024).into(imageView);
        });

I originally wrote the project with API 33 but later I changed it and I only tested on Android 11 and lower phones.

build.gradle (module)

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.imagegenerator'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.imagegenerator"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation("com.squareup.okhttp3:okhttp:4.10.0")
    implementation 'com.android.volley:volley:1.2.1'
    implementation 'com.github.bumptech.glide:glide:4.14.2'
    implementation 'com.squareup.picasso:picasso:2.8'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
sajad
  • 11
  • 3

0 Answers0