1

i've started a nativescript course on coursera . when i want to send a http request to my json-server i get the following error in my android phone: Error 0:unknown error http failure response for ..

export const  baseURL="http://10.0.2.2:3000/";

my dishes service:

@Injectable({
  providedIn: 'root'
})
export class DishService {

  constructor(private http: HttpClient,
    private processHTTPMsgService: ProcessHTTPMsgService) { }

  getDishes(): Observable<Dish[]> {
    return this.http.get<Dish[]>(baseURL + 'dishes')
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }

  getDish(id: string): Observable<Dish> {
    return this.http.get<Dish>(baseURL + 'dishes/' + id)
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }

  getFeaturedDish(): Observable<Dish> {
    return this.http.get<Dish[]>(baseURL + 'dishes?featured=true').pipe(map(dishes => dishes[0]))
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }

msg service:

export class ProcessHTTPMsgService {

  constructor() { }

  public handleError(error: HttpErrorResponse | any) {
    let errMsg: string;

    if (error.error instanceof HttpErrorResponse) {
      errMsg = error.error.message;
    } else {
      errMsg = `${error.status} - ${error.statusText || ''} ${error.message}`;
    }

    return throwError(errMsg);
  }
}
  

menu component:

export class MenuComponent implements OnInit {

  dishes: Dish[];
  errMess: string;

  constructor(private dishService: DishService,
    @Inject('baseURL') private baseURL) { }


  ngOnInit() {
    this.dishService.getDishes()
      .subscribe(dishes => this.dishes = dishes,
        errmess => this.errMess = <any>errmess);
  }

menu component html:

<ActionBar title="Menu" class="action-bar">
</ActionBar>
<StackLayout class="page">
    <ListView [items]="dishes" class="list-group" *ngIf="dishes">
        <ng-template let-dish="item">
            <StackLayout orientation="horizontal" class="list-group-item">
                <Image row="0" col="0" rowSpan="2" height="108" width="108"  [src]="baseURL + dish.image"  class="thumb p-16"></Image>
                <GridLayout class="list-group-item" rows="auto *" columns="*">
                    <Label row="0" col="0" [text]="dish.name" class="list-group-item-heading"></Label>
                    <Label row="1" col="0" class="list-group-item-text" [text]="dish.description"></Label>   
                </GridLayout>
            </StackLayout>
        </ng-template>
    </ListView>
    <ActivityIndicator busy="true"  *ngIf="!(dishes || errMess)" width="50" height="50" class="activity-indicator"></ActivityIndicator>
    <Label *ngIf="errMess" [text]="'Error: ' + errMess"></Label>
</StackLayout>

i've trie "http://127.0.0.1:3000/" also but i get the same error,i've also added android:usesCleartextTraffic="true" to my androidmanifest.xml at application section. can anyone help please it's freaking me out

Mond666
  • 121
  • 12

2 Answers2

0

Maybe try showing the entire error to get more info.
errMsg = error.error
And take a look to the console to see if there are some other errors

vill3m
  • 1
  • 3
  • i tried that and it shows Error:Error:java.net.ConnectException: Connection timed out.is it because of cors headers from server?im using json-server to return .json file.does my laptop or phone needs internet connection? – Mond666 Jul 23 '20 at 13:40
  • in error.error the message shown is :java.net.SocketException:Software caused connection abort..so what do you think about this problem? – Mond666 Jul 24 '20 at 10:46
0

I don’t see any problem with your code above.

It sounds like you’re struggling to connect to your json server.

Have you verified your server is sending responses on the same up and endpoints? (Using postman or something similar).

Is the server also listening on the ip or just localhost? If you’re using the npm package json-server then this might be useful json-server cannot access via local IP

Are you testing using an emulator or on your device? The next step I’d take is to verify you can connect to the server using a rest client app. In either case 127.0.0.1 or localhost refer to the phone, not the computer.

  • json-server is working fine in browser .i've connected to device and baseurl is not localhost or 127.0.0.1 .i've tried with my ipv4 address but still i get the same problem . – Mond666 Jul 28 '20 at 07:52