1

Earlier i was able to call api using this code and but now it is not working I'm not sure that what went wrong so if you can help it will be great. i have uploaded my code files of circulareNotice.java,api.java and Retrofitclient.java check the code and let me know that what is error and also solution of that error.

circularNotice.java

package com.example.college_connect_faculty;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.textclassifier.ConversationActions;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class circularNotice extends AppCompatActivity {
    EditText noticetxt,noticeTitle;
    Button noticeBtn;
    TextView stringLength;
    String topic = "com.example.college_connect_faculty";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FirebaseApp.initializeApp(this);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circular_notice);

        FirebaseMessaging.getInstance().subscribeToTopic("all");
        noticetxt = findViewById(R.id.noticetxt);
        noticeBtn = findViewById(R.id.noticeBtn);
        stringLength = findViewById(R.id.stringLength);
        noticeTitle = findViewById(R.id.noticeTitle);
        noticetxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = noticetxt.getText().toString();
                int l = s.length();
                stringLength.setText(String.valueOf(l));

            }
        });

        noticeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                    String notice = noticetxt.getText().toString();
                    String title = noticeTitle.getText().toString();
                    try {


                        Call<ResponseBody> call = RetrofitClient.getInstance().getApi().noticeUpload(notice, title);
                        call.enqueue(new Callback<ResponseBody>() {
                            @RequiresApi(api = Build.VERSION_CODES.Q)
                            @Override
                            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                                try {
                                    String s = response.body().string();
                                    Toast.makeText(circularNotice.this, s, Toast.LENGTH_SHORT).show();

                                    FcmNotificationsSender notificationsSender = new FcmNotificationsSender("/topics/all",
                                            title, notice, getApplicationContext(), circularNotice.this);
                                    notificationsSender.SendNotifications();
                                    FirebaseMessaging firebaseMessaging = FirebaseMessaging.getInstance();

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

                            @Override
                            public void onFailure(Call<ResponseBody> call, Throwable t) {

                            }
                        });
                    }catch (Exception e){
                        Toast.makeText(circularNotice.this,e.getMessage().toString(),Toast.LENGTH_SHORT).show();
                    }

            }
        });
    }
}

api.java

package com.example.college_connect_faculty;

import java.util.TreeMap;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;

public interface api {

    @FormUrlEncoded
    @POST("uploadSubjects.php")
    Call<ResponseBody>uploadSubject(
            @Field("semester") String semester,
            @Field("branch") String branch,
            @Field("subject")String subject
            );
    @FormUrlEncoded
    @POST("showSubjects.php")
    Call<ResultSubject> showSubject(
            @Field("semester") String semester,
            @Field("branch") String branch
    );
    @Multipart
    @POST("filesUpload.php")
    Call<ResponseBody> fileUpload(
            @Part MultipartBody.Part fileName,
            @Part("branch") RequestBody branch,
            @Part("semester") RequestBody semester,
            @Part("subject") RequestBody subject

    );

    @FormUrlEncoded
    @POST("noticeUpload.php")
    Call<ResponseBody> noticeUpload(
                    @Field("notice") String notice,
                    @Field("title") String title
    );
}

Retrofitclient.java

package com.example.college_connect_faculty;

import android.content.Context;

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

public class RetrofitClient {
    static Context context;


    final String BASE_URL = "http://192.168.153.223/college_connect/";
    private static RetrofitClient mInstance;
    Retrofit retrofit;

    RetrofitClient(){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public static synchronized RetrofitClient getInstance(){
        if (mInstance==null){
            mInstance = new RetrofitClient();
        }
        return mInstance;
    }

    public api getApi(){
        return retrofit.create(api.class);
    }

}

i have checked my ipv4. No problems in it

Moeez
  • 494
  • 9
  • 55
  • 147

1 Answers1

0

refer to this answer for a solution https://stackoverflow.com/a/53984915/5035015

for more information regarding Network security configuration, refer to the below link https://developer.android.com/training/articles/security-config

Kishor Ramani
  • 607
  • 1
  • 6
  • 12